-- Scrape all URLs from all tabs in all windows-- https://forum.keyboardmaestro.com/t/save-and-restore-safari-session-tabs/15036/4set AppleScript's text item delimiters to linefeedtell application "Safari"set urlList to (URL of tabs of windows) as textend tellreturn urlList
-- Expand Typinator abbreviationtell application "Typinator"expand string "dd"end tell
-- Print URL of current tabtell app "safari" to url of document 1
-- Show source of current documenttell app "safari" to source of document 1
-- Activate menu bar of an app-- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`-- Execute the specified menu item. In this case, assuming the Finder-- is the active application, arranging the frontmost folder by date.on menu_click(mList)local appName, topMenu, r-- Validate our inputif mList's length < 3 then error "Menu list is not long enough"-- Set these variables for clarity and brevity later onset {appName, topMenu} to (items 1 through 2 of mList)set r to (items 3 through (mList's length) of mList)-- This overly-long line calls the menu_recurse function with-- two arguments: r, and a reference to the top-level menutell application "System Events" to my menu_click_recurse(r, ((process appName)'s ¬(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))end menu_clickon menu_click_recurse(mList, parentObject)local f, r-- `f` = first item, `r` = rest of itemsset f to item 1 of mListif mList's length > 1 then set r to (items 2 through (mList's length) of mList)-- Either actually click the menu item, or recurse againtell application "System Events"if mList's length is 1 thenclick parentObject's menu item felsemy menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))end ifend tellend menu_click_recurse-- Example of usetell application "Safari" to activatemenu_click({"Safari", "Bookmarks", "Edit Bookmarks"})
-- Get path of appPOSIX path of (path to application "Sublime Text")
-- Assign variable to system variableset var1 to (system attribute "var1")
-- Trigger a keypress-- http://macbiblioblog.blogspot.com/2014/12/key-codes-for-function-and-special-keys.html-- Trigger caps lock and tilda togethertell application "System Events" to key code 50 using {option down, control down}-- Trigger ⌘ + atell application "System Events" to keystroke "a" using command down-- Trigger ⏎tell application "System Events" to key code 36
-- Run KM macrotell application "Keyboard Maestro Engine"do script "w: github"end tell
-- Capture first argument to script-- Need to wrap it in run argv clauseon run argvtell application "System Events"-- Type out the first argumentkeystroke (item 1 of argv)end tellend run
-- Type somethingtell application "System Events"keystroke "Hello"end tell
-- Get path of open document in an app-- Change "MacDown" to another apptell application "MacDown" to set file_path to file of front document
-- Get URL of open Safari tabtell application "Safari" to return URL of front document
-- Convert to POSIX pathset inputPath to POSIX path of input
-- Select first result from google search and load it in the front tab.-- Works on Safari only.set xpathStr to "//*[@class=\\'r\\']/a"set strJS to "var xpathResults = document.evaluate('" & xpathStr & "', document, null, 0, null),nodeList = [],oNode;while (oNode = xpathResults.iterateNext()) {nodeList.push(oNode.href);}nodeList;"tell application "Safari"set linkList to (do JavaScript strJS in front document)if linkList ≠ {} thenset firstLink to item 1 of linkListset URL of front document to firstLinkend ifend tell
-- Search Alfred with queryosascript -e "tell application \"Alfred 4\" to search \"$*\""
-- Get number of Safari windowstell application "Safari"set numberOfSafariWindows to length of (get current tab of windows)end tell
-- Search for tag in finderon run argvset theQuery to first item of argvtell application "Finder"make new Finder windowactivateend telldelay 0.2tell application "System Events" to tell process "Finder"keystroke "f" using command downdelay 0.1keystroke "tag:"keystroke theQuerykey code 36 -- Returnend tellend run
-- Check if currently running app is full screentell application "System Events"tell (first application process whose frontmost is true)tell (first window whose subrole is "AXStandardWindow")value of attribute "AXFullScreen"end tellend tellend tell
-- Set front window of front app to full-screentrytell application "System Events"tell (first application process whose frontmost is true)tell (first window whose subrole is "AXStandardWindow")set fullScreen to value of attribute "AXFullScreen"if fullScreen = false thenset value of attribute "AXFullScreen" to trueend ifend tellend tellend tellon error e number nset e to e & return & return & "Num: " & nif n ≠ -128 thentrytell application (path to frontmost application as text) to set ddButton to button returned of ¬(display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬default button "OK" giving up after 30)if ddButton = "Copy Error Message" then set the clipboard to eend tryend ifend try
-- Set clipboard toset the clipboard to "Some text"
-- cd to passed in dir in iTermon run argvset theQuery to item 1 of argvset cmdStr to "cd " & theQuerytell application "iTerm"if exists window 1 thentell current windowtell current session to write text cmdStrend tellelsecreate window with default profiletell current windowtell current session to write text cmdStrend tellend ifend tellend run
-- Run shell command in sudo modedo shell script "command here" with administrator privileges