URL: Choose File Dialog

The scripting implementation of the URL class contains a method for displaying a modal file chooser dialog to allow the script user to select a file on disk. The method that summons the dialog choose() takes an optional parameter in the form of a UTI (Uniform Type Identifier) string, like: “public.text”, “public.movie”, “public.spreadsheet”, “public.rtf”, “public.comma-separated-values-text”, “public.image”, or “public.png”. Providing a UTI will limit the type of items selectable by the user. By default, only a single item can be chosen within the dialog.

The example script shown below enables the user to select an image file:

chosenFile = URL.choose('public.image')
omnigraffle:///omnijs-run?script=chosenFile%20%3D%20URL.choose('public.image')

The result of the dialog will be either a URL object representing the chosen item or a null value if the user cancelled the dialog.

TIP: If your script includes the choose() method in order to have the user select an image file, in macOS, the default system Photos library can be accessed by selecting the Photos option in the Media category listed on the left side of the chooser window:

choose-photo

Import a Chosen Image File

The following example script demonstrates how to import a chosen image file into an OmniGraffle document.

The dialog presented by the choose() method (line 1) is “modal,” which means the script will not continue until the user has either selected a file or performed a tap|click on the dialog’s Cancel button. A positive selection returns the related URL object, and a cancellation returns a null value, that may be used as an indication to throw an error in order to stop the script (line 2).

Lines 3 through 5 of the script are designed to extract a URL string from the URL object, and to derive the unencoded file name of the chosen image.

imageURL = URL.choose('public.image') if (imageURL == null){throw new Error('user cancelled')} urlString = imageURL.string var imageFileName = urlString.substr(urlString.lastIndexOf('/') + 1) imageFileName = decodeURIComponent(imageFileName) imageURL.fetch(function(data){ aGraphic = canvases[0].newShape() aGraphic.strokeThickness = 0 aGraphic.shadowColor = null aGraphic.fillColor = null aGraphic.image = addImage(data) aGraphic.name = imageFileName imageSize = aGraphic.image.originalSize imgX = imageSize.width imgY = imageSize.height aGraphic.geometry = new Rect(aGraphic.geometry.x, aGraphic.geometry.y, imgX, imgY) document.windows[0].selection.view.select([aGraphic]) })
omnigraffle:///omnijs-run?script=imageURL%20%3D%20URL.choose('public.image')%0Aif%20(imageURL%20%3D%3D%20null)%7Bthrow%20new%20Error('user%20cancelled')%7D%0AurlString%20%3D%20imageURL.string%0Avar%20imageFileName%20%3D%20urlString.substr(urlString.lastIndexOf('%2F')%20%2B%201)%0AimageFileName%20%3D%20decodeURIComponent(imageFileName)%0AimageURL.fetch(function%20(data)%20%7B%0A%09aGraphic%20%3D%20canvases%5B0%5D.newShape()%0A%09aGraphic.strokeThickness%20%3D%200%0A%09aGraphic.shadowColor%20%3D%20null%0A%09aGraphic.fillColor%20%3D%20null%0A%09aGraphic.image%20%3D%20addImage(data)%0A%09aGraphic.name%20%3D%20imageFileName%0A%09imageSize%20%3D%20aGraphic.image.originalSize%0A%09imgX%20%3D%20imageSize.width%0A%09imgY%20%3D%20imageSize.height%0A%09aGraphic.geometry%20%3D%20new%20Rect(aGraphic.geometry.x%2C%20aGraphic.geometry.y%2C%20imgX%2C%20imgY)%0A%09document.windows%5B0%5D.selection.view.select(%5BaGraphic%5D)%0A%7D)

The importation of the image file into the document is accomplished by using the fetch() method (line 6 — 18), an asynchronous handler that employs a call-back function to provide the means for dealing with the imported image. In other words, the fetch process runs separate from any remaining script statements, and when done with its import, executes the call-back function passed to it as its direct parameter. The call-back function contains the code for creating and manipulating the graphic that contains the image.

Note the use of the addImage() method (line 11) to insert the image data passed to the call-back function by the fetch() method, into the graphic object previously created using the newShape() method (line 7). The created graphic is named to match the image file name (line 12), and then its dimensions are adjusted to match the default size of the imported image (lines 13 — 16).

The last script statement selects the created image graphic in the document (line 17).

UNDER CONSTRUCTION

This webpage is in the process of being developed. Any content may change and may not be accurate or complete at this time.

DISCLAIMER