URL: Choose Folder Dialog

Sometimes Omni Automation scripts require user input in order to perform their designated tasks. For example, having the script user choose a directory containing data or files needed by the script. The chooseFolder() method of the URL class will display a folder picker dialog from witch the user can select a specific directory.

In the following script example, the chooseFolder() method is used to identify the folder containing image files whose file names match the metadata tags of tagged graphic containers in an OmniGraffle document. Once the directory has been selected by the user, the matching images are imported into their corresponding containers.

Try it yourself! Download the example images and OmniGraffle document with tagged containers, and run the following script:

var cnvs = document.windows[0].selection.canvas var dataKey = 'SKU' var fileExtension = 'JPG' // get a list of tag values from graphics tagged with SKU metadata tag keyValues = new Array() cnvs.graphics.forEach(function(graphic){ keyValue = graphic.userData[dataKey] if(keyValue != null){keyValues.push(keyValue)} }) var valCount = keyValues.length if(valCount == 0){ new Alert('MISSING DATA KEYS','No graphics are assigned the data key:\n\n\t' + dataKey).show(function(result){throw new Error('script cancelled')}) } else { fldrURL = URL.chooseFolder() if (fldrURL == null){throw new Error('user cancelled')} fldrURLstr = fldrURL.string keyValues.forEach(function(keyValue){ aStr = fldrURLstr + keyValue + '.' + fileExtension fileURL = URL.fromString(aStr) fileURL.fetch((function(keyValue, data) { aGraph = cnvs.graphicWithUserDataForKey(keyValue, dataKey) aGraph.image = addImage(data) }).bind(this, keyValue)) }) }
omnigraffle:///omnijs-run?script=var%20cnvs%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%0Avar%20dataKey%20%3D%20%27SKU%27%0Avar%20fileExtension%20%3D%20%27JPG%27%0AkeyValues%20%3D%20new%20Array%28%29%0Acnvs%2Egraphics%2EforEach%28function%28graphic%29%7B%0A%09keyValue%20%3D%20graphic%2EuserData%5BdataKey%5D%0A%09if%28keyValue%20%21%3D%20null%29%7BkeyValues%2Epush%28keyValue%29%7D%0A%7D%29%0Avar%20valCount%20%3D%20keyValues%2Elength%0Aif%28valCount%20%3D%3D%200%29%7B%0A%09new%20Alert%28%27MISSING%20DATA%20KEYS%27%2C%27No%20graphics%20are%20assigned%20the%20data%20key%3A%5Cn%5Cn%5Ct%27%20%2B%20dataKey%29%2Eshow%28function%28result%29%7Bthrow%20new%20Error%28%27script%20cancelled%27%29%7D%29%0A%7D%20else%20%7B%0A%09fldrURL%20%3D%20URL%2EchooseFolder%28%29%0A%09if%20%28fldrURL%20%3D%3D%20null%29%7Bthrow%20new%20Error%28%27user%20cancelled%27%29%7D%0A%09fldrURLstr%20%3D%20fldrURL%2Estring%0A%09keyValues%2EforEach%28function%28keyValue%29%7B%0A%09%09aStr%20%3D%20fldrURLstr%20%2B%20keyValue%20%2B%20%27%2E%27%20%2B%20fileExtension%0A%09%09fileURL%20%3D%20URL%2EfromString%28aStr%29%0A%09%09fileURL%2Efetch%28%28function%28keyValue%2C%20data%29%20%7B%0A%09%09%09aGraph%20%3D%20cnvs%2EgraphicWithUserDataForKey%28keyValue%2C%20dataKey%29%0A%09%09%09aGraph%2Eimage%20%3D%20addImage%28data%29%0A%09%09%7D%29%2Ebind%28this%2C%20keyValue%29%29%0A%09%7D%29%0A%7D

TIP: For a more detailed explanation of how to use metadata with scripts in OmniGraffle, view this page.

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