Skip to content

Instantly share code, notes, and snippets.

@ChristoferK
Last active April 25, 2019 15:18
Show Gist options
  • Save ChristoferK/dcf5e7ecbdeb09aa2544dacf6911ec43 to your computer and use it in GitHub Desktop.
Save ChristoferK/dcf5e7ecbdeb09aa2544dacf6911ec43 to your computer and use it in GitHub Desktop.
[Redefining AppleScript Builtins] A series of command declarations that surplant the builtin definitions of some common AppleScript commands to increase versatility and functionality. #AppleScript #builtins #handlers #recursion #date #clipboard
on clipboard info
set cb to continue clipboard info
repeat with x in cb
set [contents of x] to x
end repeat
cb
end clipboard info
on current date
continue current date as «class isot» as string
set [d, t] to [text 1 thru 10, text 12 thru -4] of the result
d & space & t
end current date
to do shell script input
if the input's class = list then
set [tid, my text item delimiters] to ¬
[my text item delimiters, space]
set the input to input as text
set my text item delimiters to tid
end if
continue do shell script input
end do shell script
to load script f as text
local f
script
property sys : application "System Events"
property fp : «class posx» of sys's file f
property tmp : "~/Desktop/tmp.scpt"
to load()
if fp ends with ".scpt" then return fp
do shell script ¬
"osacompile -o " & tmp & ¬
space & fp's quoted form
tmp
end load
end script
continue load script result's load()
end load script
script recursive
on offset of txt in str
local txt, str
if txt is not in str then return {}
script
property s : characters of str
property t : characters of txt
end script
tell the result to set [rs, rt] to [¬
(reverse of its s) as text, ¬
(reverse of its t) as text]
set i to 1 + (str's length) - (continue offset of rt in rs)
my (offset of txt in str's text 1 thru (i - 1)) & ¬
(i - (txt's length) + 1)
end offset
end script
script iterative
on offset of txt in str
local txt, str
if txt is not in str then return {}
set the text item delimiters to txt
script
property len : txt's length
property s : str's text items
property t : {1 - len} & s
end script
tell the result
repeat with i from 2 to (its t's length) - 1
set its t's item i to (its len) + ¬
(length of its t's item i) + ¬
(its t's item (i - 1))
end repeat
items 2 thru -2 of its t
end tell
end offset
end script
iterative's (offset of "*" in "12*45678*0*BC*EF*")
--> {3, 9, 11, 14, 17}
recursive's (offset of "pen" in "Don't shove a pencil down your penis.")
--> {15, 32}
on path to f
local f
if f's class ≠ text then return the POSIX path of (continue path to f)
tell application "System Events" to get alias f as alias
POSIX path of result
end path to

A series of command declarations that surplant the builtin definitions of some common AppleScript commands to increase versatility and functionality:

  • do shell script Supply the shell command string optionally as a list of strings that will be concatenated and delimited using a space character

  • load script Use scripts stored in text files that can be referenced by posix paths utilising ~/, or any other type of AppleScript file reference (e.g. POSIX file, file, alias)

  • offset Return a list of all offsets of a substring. Includes recursive and iterative routines, the latter being the most efficient.

  • path to Returns posix paths. Accepts posix paths that are standardised before returning the full path of a file or folder.

  • current date The current date and formatted according to ISO-8601 standards, i.e. yyyy-mm-dd hh:mm:ss

  • clipboard info A list of data types contained on the clipboard (without their byte sizes)

These can be especially useful when defined within a script object, although top-level declarations may be warranted, in which case these will become the default handler when called. To make use of the original builtin, use continue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment