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 an array of UTI (Uniform Type Identifier) strings, like: “public.text”, “public.movie”, “public.spreadsheet”, “public.rtf”, “public.comma-separated-values-text”, “public.image”, or “public.png”. Providing UTIs will limit the type of items selectable by the user.

NOTE: 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'])

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:


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]) })

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).


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