AppleScript

Notes

  • You can drop an application on Apple's Script Editor.app to find out if it is scriptable.

Code

1
-- Scrape all URLs from all tabs in all windows
2
-- https://forum.keyboardmaestro.com/t/save-and-restore-safari-session-tabs/15036/4
3
set AppleScript's text item delimiters to linefeed
4
tell application "Safari"
5
set urlList to (URL of tabs of windows) as text
6
end tell
7
return urlList
Copied!
1
-- Expand Typinator abbreviation
2
tell application "Typinator"
3
expand string "dd"
4
end tell
Copied!
1
-- Print URL of current tab
2
tell app "safari" to url of document 1
Copied!
1
-- Show source of current document
2
tell app "safari" to source of document 1
Copied!
1
-- Activate menu bar of an app
2
​
3
-- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
4
-- Execute the specified menu item. In this case, assuming the Finder
5
-- is the active application, arranging the frontmost folder by date.
6
on menu_click(mList)
7
local appName, topMenu, r
8
​
9
-- Validate our input
10
if mList's length < 3 then error "Menu list is not long enough"
11
​
12
-- Set these variables for clarity and brevity later on
13
set {appName, topMenu} to (items 1 through 2 of mList)
14
set r to (items 3 through (mList's length) of mList)
15
​
16
-- This overly-long line calls the menu_recurse function with
17
-- two arguments: r, and a reference to the top-level menu
18
tell application "System Events" to my menu_click_recurse(r, ((process appName)'s Β¬
19
(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
20
end menu_click
21
​
22
on menu_click_recurse(mList, parentObject)
23
local f, r
24
​
25
-- `f` = first item, `r` = rest of items
26
set f to item 1 of mList
27
if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
28
​
29
-- Either actually click the menu item, or recurse again
30
tell application "System Events"
31
if mList's length is 1 then
32
click parentObject's menu item f
33
else
34
my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
35
end if
36
end tell
37
end menu_click_recurse
38
​
39
-- Example of use
40
tell application "Safari" to activate
41
​
42
menu_click({"Safari", "Bookmarks", "Edit Bookmarks"})
Copied!
1
-- Get path of app
2
POSIX path of (path to application "Sublime Text")
Copied!
1
-- Assign variable to system variable
2
set var1 to (system attribute "var1")
Copied!
1
-- Trigger a keypress
2
-- http://macbiblioblog.blogspot.com/2014/12/key-codes-for-function-and-special-keys.html
3
​
4
-- Trigger caps lock and tilda together
5
tell application "System Events" to key code 50 using {option down, control down}
6
​
7
-- Trigger ⌘ + a
8
tell application "System Events" to keystroke "a" using command down
9
​
10
-- Trigger ⏎
11
tell application "System Events" to key code 36
Copied!
1
-- Run KM macro
2
tell application "Keyboard Maestro Engine"
3
do script "w: github"
4
end tell
Copied!
1
-- Capture first argument to script
2
​
3
-- Need to wrap it in run argv clause
4
on run argv
5
tell application "System Events"
6
-- Type out the first argument
7
keystroke (item 1 of argv)
8
end tell
9
end run
Copied!
1
-- Type something
2
tell application "System Events"
3
keystroke "Hello"
4
end tell
Copied!
1
-- Get path of open document in an app
2
​
3
-- Change "MacDown" to another app
4
tell application "MacDown" to set file_path to file of front document
Copied!
1
-- Get URL of open Safari tab
2
tell application "Safari" to return URL of front document
Copied!
1
-- Convert to POSIX path
2
set inputPath to POSIX path of input
Copied!
1
-- Select first result from google search and load it in the front tab.
2
-- Works on Safari only.
3
​
4
set xpathStr to "//*[@class=\\'r\\']/a"
5
set strJS to "
6
​
7
var xpathResults = document.evaluate('" & xpathStr & "', document, null, 0, null),
8
nodeList = [],
9
oNode;
10
​
11
while (oNode = xpathResults.iterateNext()) {
12
nodeList.push(oNode.href);
13
}
14
​
15
nodeList;
16
​
17
"
18
tell application "Safari"
19
set linkList to (do JavaScript strJS in front document)
20
​
21
if linkList β‰  {} then
22
set firstLink to item 1 of linkList
23
set URL of front document to firstLink
24
end if
25
​
26
end tell
Copied!
1
-- Search Alfred with query
2
osascript -e "tell application \"Alfred 4\" to search \"$*\""
Copied!
1
-- Get number of Safari windows
2
tell application "Safari"
3
set numberOfSafariWindows to length of (get current tab of windows)
4
end tell
Copied!
1
-- Search for tag in finder
2
on run argv
3
set theQuery to first item of argv
4
​
5
tell application "Finder"
6
make new Finder window
7
activate
8
end tell
9
delay 0.2
10
tell application "System Events" to tell process "Finder"
11
keystroke "f" using command down
12
delay 0.1
13
keystroke "tag:"
14
keystroke theQuery
15
key code 36 -- Return
16
end tell
17
end run
Copied!
1
-- Check if currently running app is full screen
2
tell application "System Events"
3
tell (first application process whose frontmost is true)
4
tell (first window whose subrole is "AXStandardWindow")
5
​
6
value of attribute "AXFullScreen"
7
end tell
8
end tell
9
end tell
Copied!
1
-- Set front window of front app to full-screen
2
try
3
tell application "System Events"
4
tell (first application process whose frontmost is true)
5
tell (first window whose subrole is "AXStandardWindow")
6
​
7
set fullScreen to value of attribute "AXFullScreen"
8
​
9
if fullScreen = false then
10
set value of attribute "AXFullScreen" to true
11
end if
12
​
13
end tell
14
end tell
15
end tell
16
​
17
on error e number n
18
set e to e & return & return & "Num: " & n
19
if n β‰  -128 then
20
try
21
tell application (path to frontmost application as text) to set ddButton to button returned of Β¬
22
(display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} Β¬
23
default button "OK" giving up after 30)
24
if ddButton = "Copy Error Message" then set the clipboard to e
25
end try
26
end if
27
end try
Copied!
1
-- Set clipboard to
2
set the clipboard to "Some text"
Copied!
1
-- cd to passed in dir in iTerm
2
on run argv
3
set theQuery to item 1 of argv
4
set cmdStr to "cd " & theQuery
5
tell application "iTerm"
6
if exists window 1 then
7
tell current window
8
tell current session to write text cmdStr
9
end tell
10
else
11
create window with default profile
12
tell current window
13
tell current session to write text cmdStr
14
end tell
15
end if
16
end tell
17
end run
Copied!
1
-- Run shell command in sudo mode
2
do shell script "command here" with administrator privileges
Copied!

Links

Last modified 1mo ago
Copy link