Tips for golfing in Applescript

What tips do you have for golfing in Applescript? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to Applescript (e.g. "remove comments" is not an answer).

• This question is Too Broad. It can't possibly be answered with a one complete, well-written answer (and the question even explicitly forbids that). – Rainbolt Sep 11 '14 at 16:55
• Still, it's just a funny question :) – MadTux Sep 11 '14 at 17:26
• Flagged for conversion to CW (per our convention for 'tips for golfing in x' questions). – Geobits Sep 11 '14 at 17:32
• @MadTux Yep, Applescript is not usually the 1st choice in golfing languages ;-). But there are occasions, particularly in the restricted-source category where it becomes marginally useful. – Digital Trauma Sep 11 '14 at 17:41
• @Rainbolt How is this different in nature to other tips for golfing in x questions? – Digital Trauma Sep 11 '14 at 18:00

The of operator is used to get a property from an object:

log words of "Hello World"


But in many cases you can use 's as a shortcut to save 1 character:

log "Hello World"'s words

• It seems somewhat ridiculous, but also somewhat awesome, that Applescript parses apostrophes as possessive. I can just imagine one of the language designers pitching that idea to the group. – Geobits Sep 11 '14 at 17:42
• @Geobits Those are great adjectives for describing Applescript in general. I just haven't decided if Applescript is ridiculously awesome or awesomely ridiculous ;-) – Digital Trauma Sep 11 '14 at 17:44

The Applescript Editor is a handy little IDE which syntax-highlights and beautifies your code. However, for the purposes of golfing, it is counterproductive as it adds indentation and superfluous keywords, e.g. after end statements. For example:

repeat with w in "Hello World"'s words
log w
end


becomes the following when pasted into the Applescript Editor and compiled/run:

repeat with w in "Hello World"'s words
log w
end repeat


Obviously the first snippet is better for the purposes of golfing.

• IMO this can be seen as a general tip, not specific to AS. Many modern IDEs do this if you let them. – Geobits Sep 11 '14 at 17:30
• @Geobits Agreed. Though I think the addition of specific keywords after end is fairly Applescript specific. – Digital Trauma Sep 11 '14 at 17:31
• Yea, that's just baffling to me. The indentation/spacing thing is very common, though, along with unnecessary brackets, etc. – Geobits Sep 11 '14 at 17:33
• @Geobits Applescript is all about readability. I guess the idea is to make it clear which end belongs to which block start statement when you have multiple nested blocks – Digital Trauma Sep 11 '14 at 17:34

tell blocks are common in Applescript:

tell application "TextEdit"
activate
end tell


However to save space the following is equivalent, when the inside of the tell block is just one line:

tell application "TextEdit" to activate

• Which itself is tell app "TextEdit" to activate. – wchargin Sep 12 '14 at 4:16
• @WChargin Yes. Add it as another answer if you like. – Digital Trauma Sep 12 '14 at 18:06

Some words have shorter synonyms. For example, application can be written as app*, and string can be written as text.

Also, every <noun> can be written as simply the plural, as in characters of "hello world" (or "foo"'s characters).

*Although Script Editor's compiler will change it back.

Quotation Required Operation

For any operation that requires a quote to do something, i.e.

log "Hello World!"

You can shorten to

log"Hello World!"

Repeating

In repeat loops, one can entirely remove the word "times".

repeat x times
end

versus

repeat x
end

<= and >=

Any time these operators are called, you can replace them with ≤ and ≥, respectively. While this may not reduce byte count (unless special byte counting conventions are implemented, which I suggest), it does reduce character count.

Grabbing from STDIN

You can grab from STDIN in with the following characters:

on run argv
end

Exiting quickly

If you need to exit a code quickly (for whatever reason, i.e. preventing excessive ifs)...

quit
• Haha wow, you must have done some trawling to find this ;) – Beta Decay Oct 16 '15 at 18:20
• I use it a lot. – Addison Crump Oct 16 '15 at 18:22
• The question or the tips? – Beta Decay Oct 16 '15 at 18:22
• The tips. And AppleScript in general. – Addison Crump Oct 16 '15 at 18:24
• Oh I was talking about the question :) – Beta Decay Oct 16 '15 at 18:24

Applescript allows some extra keywords to be inserted to help readability:

log the words of "Hello World"


But the the here is completely superfluous and may be omitted for a 4 character saving:

log words of "Hello World"

• You can have even shorter variants: log"Hello World"'s words, because you 1) don't need the space between any expression and quoted item, and 2) because 's can be used in replacement of of. – Addison Crump Oct 17 '15 at 21:55
• @VTCAKAVSMoACE yes, 's is addressed in this answer codegolf.stackexchange.com/a/37643/11259. Good to know about log" though. – Digital Trauma Oct 17 '15 at 22:57

Bracket Shortening

Similar to the post about quotations, I realized later that you can also shorten things like this:

if "a"=character 1 of (x as string) then return {true, true}

to

if"a"=character 1 of(x as string)then return{true,true}

It'll space out brackets for you too. In this example, I save 5 bytes.

Considering...

In questions that require case sensitivity, it can be difficult to actually deal with cases.

UNTIL NOW:

considering case
(something to do with case sensitive stuff)
end considering

I didn't actually know this keyword until I really needed it. Using the other tips in this tips page, we can reduce this down to:

considering case
(something to do with case sensitive stuff)
end

It does require a full statement, as far as I know. (I've tried a lot of things.) See this page for more details.

In a few exceptional cases, the «double angle brackets» or «double chevrons» might be shorter than the English name of a command, parameter, or constant.

The chevron-encoded form would shrink the AppleScript to delete the clipboard from 20 to 16 characters:

set the clipboard to  -- 20
«event JonspClp»      -- 16


It would drop 2 characters when fetching text from a dialog (as happens in metronome and Pi Day):

(display dialog""default answer"")'s text returned  -- 46
(display dialog""default answer"")'s«class ttxt»    -- 44


(You might prefer to avoid the dialog and use the command-line arguments of osascript(1), if you have at least Mac OS X 10.4.)

With a US keyboard, « is option-\ and » is option-shift-\. A command «event abcdefgh» has 16 characters. A parameter or constant «class abcd» has 12 characters.

The chevron-encoded form is almost always too long for golf. Here is an example without double angle brackets:

set x to open for access"output"write permission 1
write"One line of text
"to x
close access x


And the same with them:

set x to«event rdwropen»"output"given«class perm»:1
«event rdwrwrit»"One line of text
"given«class refn»:x
«event rdwrclos»x


Changing _open for access (16) to «event rdwropen» (16) was neutral. Changing write permission_ (17) to given«class perm»: (18) cost 1 character. The other double angle brackets cost more.

To use double angle brackets, you need to know the magic 4-letter or 8-letter code. I found some codes by saving a script file from Script Editor, then opening it in a hex editor. I ran emacs in a terminal and used M-x hexl-find-file. I found and edited some codes, like JonspClp into JanspClp, and ttxt into atxt. I then saved the file and reopened it in Script Editor. «event JanspClp» and «class atxt» appeared in the script.

A document titled AppleScript Terminology and Apple Event Codes Reference lists some codes. I found a copy of it at https://applescriptlibrary.wordpress.com/

Script Editor will translate double angle brackets to English before saving your script. For chevron deference, you must write your script in another text editor, like TextEdit. Save the script as a plain text file in the Mac OS Roman (or MacRoman) encoding. Mac OS X prefers that you name the file with an .applescript suffix.

If you count bytes, MacRoman has 1 byte per character, so each « or » counts as 1 byte.

• Careful though, « and » are 0xc2ab and 0xc2bb in UTF-8 - i.e. 2 bytes each. – Digital Trauma Jan 25 at 20:47
• An especially useful variant is replacing text item delimiters with «class txdl». – Digital Trauma Jan 25 at 21:48