OpenIn 4
Documentation - 08. JavaScript Editor for Rules
Table of content
Working with the Script Editor
You can configure a test URL to work with, and be able to Run the script to see the Result and Console Log output.
Examples
Depending on the type of the rule you are editing and for which handler, you can find some examples in the menu list under the Script Editor.
Available objects
ctx
object is available for the script.
With the following methods and properties
url
- returns the URL objectgetSourceApp()
- returnsOpenContextSourceApp
object, that can tell you if OpenIn recognized the application where link was openedgetApps()
- returns the array of apps, where OpenIn logic already applied, but you can override it, array ofOpenContextApp
isForPrinting()
- (bool
) check if file was requested to be opened for printinggetModifiers()
- returnsOpenContextModifiers
object, that can tell which modifiers are pressed when link or file was requested to be opened by userisShareMenu()
- (bool
) check if the link was sent by the Share Menu Open in OpenInisHandoff()
- (bool
) check if the link was sent by the Handoff in OpenIngetFocusHint()
- (string
) get focus hint configured with the System Settings Focus (empty string if none is configured)
OpenContextSourceApp
provides the following properties:
path
- (string
) the path of the application, that sent an open request
You can check if the opening app is Slack or other app. Just a note, when you open links from Slack, it is going to be a
different app, that is under /Applications/Slack.app/...
, so to check if that is Slack app, you want to check if this path starts with Slack.app path.
OpenContextApp
provides the following properties:
name
- the name of the app (string
)running
- check if app is running (bool
)bundleIdentifier
- the bundle identifier of the app (string
)visible
- (bool
) you can get or set this property, that is where you can apply your logic, for the list of the apps,- that you are getting from OpenIn, you can configure which one you want to see in the result list
URL
provides the following properties:
fragement
- everything after#
(string
)host
- hostname and port (string
)hostname
- just a domain, hostname (string
)href
- string representation of full url (string
)username
- username (string
)password
- password (string
)pathname
- the path (string
)port
- port (string
)protocol
- scheme (string
)search
- query of the url (string
)searchParams
- object to work on url query (SearchParams
)
SearchParams
provides the object with the following methods
append(name, value)
- append the search key valuedelete(name)
- delete all query items with the nameget(name)
- get the first value of the query with namegetAll(name)
- get all values as array of the query with namehas(name)
- check if query has a keyset(name, value)
- set the name and value for the searchkeys()
- get the array of all keys
OpenContextModifiers
providers the object with the following properties
shift
- shift pressed (bool
)option
- option pressed (bool
)command
- command pressed (bool
)
Debugging
You can use console.log("test")
to write to a console output.
Examples
Example of the custom rule, that make decisions based on where the link is opened, and domains
(function() {
// the list of available apps to send link to
let apps = ctx.getApps()
// if the source app (where link is opened is Slack,
// keep only Safari as visible app for OpenIn
// and because we will have only one app left,
// OpenIn will just open the Safari
if (ctx.getSourceApp().path.startsWith("/Applications/Slack.app")) {
apps.forEach(function (app) {
app.visible = (app.name == "Safari")
})
return
}
// all loshadki.app urls direct to Safari as well
if (url.hostname.endsWith("loshadki.app")) {
apps.forEach(function (app) {
app.visible = (app.name == "Safari")
})
return
}
// all google.com urls direct to Safari and Chrome
if (url.hostname.endsWith("google.com")) {
apps.forEach(function (app) {
app.visible = ["Safari", "Google Chrome"].includes(app.name)
})
return
}
})()