Use AppleScript in a Shell Script

Use AppleScript in a Shell Script

You can write AppleScript as Shell Scripts just as you would with any other bash/zsh file. The key requirement is that you start the file with the appropriate shebang line:

#! /usr/bin/env osascript

Then make sure you correctly grab your arguments

--grabs the first argument
set myArgument to (item 1 of argv)
#! /usr/bin/env osascript
-- usage: `open-tab twitter`
-- Opens chrome, checks if twitter is already open,
-- switches to existing tab. Opens a new tab it not.
on run argv
set address to (item 1 of argv)
tell application "Google Chrome"
activate
if not (exists window 1) then reopen
repeat with w in windows
set i to 1
repeat with t in tabs of w
if URL of t contains address then
set active tab index of w to i
set index of w to 1
return
end if
set i to i + 1
end repeat
end repeat
open location "http://" & address
end tell
end run

Get Automation Tips in Your Inbox

Sign-up to get Automation tips sent directly to your inbox to improve your daily computer life!

    No spam. Unsubscribe whenever.