up:: 05 Programming MOC




Handy Snippets

Create calendar events

set theStartDate to (current date) + (1 * days)
set hours of theStartDate to 15
set minutes of theStartDate to 0
set seconds of theStartDate to 0
set theEndDate to theStartDate + (1 * hours)
tell application "Calendar"
	tell calendar "Social"
		make new event with properties {summary:"Important Meeting!", start date:theStartDate, end date:theEndDate}
	end tell
end tell
-- This example script turns on the "iTunes Visualizer" visualizer, full screen
-- tell app "iTunes" to activate
-- menu_click({"iTunes", "View", "Visualizer", "iTunes Visualizer"})
-- menu_click({"iTunes", "View", "Turn On Visualizer"})
-- menu_click({"iTunes", "View", "Full Screen"})
-- `menu_click`, by Jacob Rus, September 2006
-- 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 input
	if mList's length < 3 then error "Menu list is not long enough"
	-- Set these variables for clarity and brevity later on
	set {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 menu
	tell 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_click
on menu_click_recurse(mList, parentObject)
	local f, r
	-- `f` = first item, `r` = rest of items
	set f to item 1 of mList
	if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
	-- either actually click the menu item, or recurse again
	tell application "System Events"
		if mList's length is 1 then
			click parentObject's menu item f
			my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
		end if
	end tell
end menu_click_recurse


do shell script "eval $(/usr/libexec/path_helper -s); cliclick c:" & textX & "," & textY

Automating User Interface

Apple Developer Documentation

key codes

List of key codes

key code 36 -- enter
key code 48 -- tab
key code 51 -- delete
key code 124 -- right
key code 125 -- down
key code 126 -- up
key code "26" using {shift down, option down} -- write "\"


set dialogResult to display dialog "Do you want to save this chapter?" with icon caution buttons {"Exit", "Save"} default button "Save" giving up after 5
	if the button returned of dialogResult is "Exit" then
		error number -128
	end if

Check if app is running

on appIsRunning(GTDAppName)
	tell application "System Events"
		return (count of (application processes whose name is GTDAppName)) is not 0
	end tell
end appIsRunning
if appIsRunning("Todoist") then
	tell application "Todoist"
		tell application "System Events" to keystroke space using {shift down, command down}
	end tell
end if


display notification "All graphics have been converted." with title "My Graphic Processing Script" subtitle "Processing is complete." sound name "Frog"