URL

A URL (Uniform Resource Locator) is a string of text-characters used to identify a file or resource, either locally on the computing device, or on a network or the internet. An essential component of HTML links, a URL is composed of text segments separated by forward-slash characters, like this URL that references an image file on an internet server:

https://omni-automation.com/gfx/18207.jpg

In OmniJS, URLs are used to reference local or networked files, often for importing into documents.

String to URL

You can create an instance of a URL object by calling the fromString() method on the URL class. This function takes the URL string as its direct parameter. In the example below, a URL string is converted into a URL object stored in the variable: aURL

var aURL = URL.fromString('https://omni-automation.com/gfx/18207.jpg')

URL to String

When working with URLs in your scripts, you may wish to extract the name of the file or resource from the URL object. This is accomplished by extracting the URL string from the URL object by accessing the value of its “string” property. Once you retrieved the URL string from the URL object, you can use standard JavaScript tools for deriving the name, as shown in this example:

var aURL = URL.fromString('https://omni-automation.com/gfx/18207.jpg') var urlString = aURL.string var resourceName = urlString.substr(urlString.lastIndexOf('/') + 1)

URL of a PlugIn Resource

Resource files place within Omni plugins are referenced using URLs. To derive the URL of a plugin resource file, use the following technique of identifying the host plugin by its identifier string, and then calling the resourceNamed() method on the plugin object:

plugin = PlugIn.find("com.NyhthawkProductions.OmniGraffle") var resourceName = "Australia.png" aURL = plugin.resourceNamed(resourceName)

Accessing the value of the returned URL object’s “string” property will be a file URL string pointing to the indicating file on disk:

file:///Users/sal/Library/Containers/com.omnigroup.OmniGraffle7/Data/Library/Application%20Support/PlugIns/SalsSnippets.omnigrafflejs/Resources/Australia.png

Retrieving a Resource

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

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

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

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:

plugin = PlugIn.find("com.NyhthawkProductions.OmniGraffle") 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]) })
omnigraffle:///omnijs-run?script=plugin%20%3D%20PlugIn.find(%22com.NyhthawkProductions.OmniGraffle%22)%0Avar%20resourceName%20%3D%20%22Australia.png%22%0Aurl%20%3D%20plugin.resourceNamed(resourceName)%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)
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