×

URL: Fetch

Omni Automation scripts can import data and content from a variety of sources. Doing so involves the use of the fetch() method, executed on a URL object instance. The fetch() function runs asynchronously in relation to the rest of its host script and so any script code for processing the results of the fetch must be placed within a call-back function that is the direct parameter of the call.

NOTE: the fetch() instance function of the URL class supports the retrieval of data (GET) but not the use of header data (POST). The fetch() function of the URL class is appended to a URL instance and takes a call-back function as its function parameter: aURL.fetch(function(retrievedData){//actions go here})

However, the URL.FetchRequest and URL.FetchResponse classes provide a version of the fetch() function that may optionally include HTTP headers to perform authenticated GET, POST, PUT, and DELETE functions.

Examples

For example, the following script uses the fetch() method to download an image file from the internet, and import it into the current OmniGraffle document. In the example, the fetch() method is called (line 4) and the processing of the downloaded image is performed in the call-back function (line 4 — 16):

omnigraffle:///omnijs-run?script=urlString%20%3D%20%22https%3A%2F%2Fomni-automation.com%2Fgfx%2F18207.jpg%22%0Avar%20resourceName%20%3D%20urlString.substr(urlString.lastIndexOf('%2F')%20%2B%201)%0AresourceName%20%3D%20decodeURIComponent(resourceName)%0Avar%20url%20%3D%20URL.fromString(urlString)%0Aurl.fetch(function(data)%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%20resourceName%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)
OmniGraffle: Import Image from Internet
 

var urlString = "https://omni-automation.com/gfx/18207.jpg" var resourceName = urlString.substr(urlString.lastIndexOf('/') + 1) resourceName = decodeURIComponent(resourceName) var url = URL.fromString(urlString) url.fetch(function(data){ aGraphic = canvases[0].newShape() aGraphic.strokeThickness = 0 aGraphic.shadowColor = null aGraphic.fillColor = null aGraphic.image = addImage(data) aGraphic.name = resourceName 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]) })

And here is another example using the same technique. This script will import an image file into the current OmniGraffle document from an installed Plugin:

OmniGraffle: Import Image from PlugIn


var plugin = PlugIn.find("com.omni-automation.og.example-plug-in") var resourceName = "Australia.png" url = plugin.resourceNamed(resourceName) url.fetch(function(data){ aGraphic = canvases[0].newShape() aGraphic.strokeThickness = 0 aGraphic.shadowColor = null aGraphic.fillColor = null aGraphic.image = addImage(data) aGraphic.name = resourceName 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]) })

Retrieving Other Data Types

The previous examples retrieved image data that was added to an OmniGraffle document as an image. The following example uses the fetch() method to retrieve textual weather data from NOAA (National Oceanic and Atmospheric Administration) in XML format that can be parsed for specific data. In this example, the current temperature at the San Francisco International Airport is retrieved and displayed in an alert:

Get Temperature at San Francisco Airport (SFO)


var aURL = URL.fromString('https://w1.weather.gov/xml/current_obs/KSFO.xml') var locationName = "San Francisco Airport" aURL.fetch(function(data){ queryResult = data.toString() startTag = '<temperature_string>' startTagOffset = queryResult.indexOf(startTag) endTag = '</temperature_string>' endTagOffset = queryResult.indexOf(endTag) tempString = queryResult.substring(startTagOffset + startTag.length, endTagOffset) new Alert(locationName,tempString).show(function(result){}) }, function(err){ new Alert(err.name, err.message).show() })
temperature-alert

URLs for other NOAA weather stations can be found in an XML document posted by NOAA.

And here’s another example that retrieves a JSON data object from the JSONPlaceholder website:

Retrieve JSON Object


var urlStr = 'https://jsonplaceholder.typicode.com/posts/1' url = URL.fromString(urlStr) url.fetch(function(data){ obj = JSON.parse(data.toString()) console.log(obj.userId) console.log(obj.id) console.log(obj.title) console.log(obj.body) })
console-json-retrieve  

The find(…) Function

The find(…) function can be used to search a specified directory for files of specified type(s):

In the following example, the user chooses a folder from a file picker dialog. Once chosen, the folder is searched, using the find(…) function, for image files saved in PNG format.

Finding PNG Images in Chosen Folder
 

// FILEPICKER INSTANCE CREATED AND PROPERTIES SET var picker = new FilePicker() picker.folders = true picker.multiple = false // PICKER PROMISE IS CREATED AND STORED WHEN PICKER IS DISPLAYED var pickerPromise = picker.show() // PROMISE FUNCTION CALLED UPON PICKER APPROVAL pickerPromise.then(function(urls){ var folderURL = urls[0] console.log(folderURL) // SEARCH THE CHOSEN FOLDER FOR FILES OF SPECIFIED TYPE(S) var searchPromise = folderURL.find([FileType.png], false) searchPromise.then(urls => { urls.forEach(url => { console.log(url.string) }) }) searchPromise.catch(err => { console.log(err.message) }) }) // PROMISE FUNCTION CALLED UPON PICKER CANCELLATION pickerPromise.catch(function(error){ console.log("form cancelled", error.message) })