13
\$\begingroup\$

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).

\$\endgroup\$
  • \$\begingroup\$ 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). \$\endgroup\$ – Rainbolt Sep 11 '14 at 16:55
  • 1
    \$\begingroup\$ Still, it's just a funny question :) \$\endgroup\$ – MadTux Sep 11 '14 at 17:26
  • 5
    \$\begingroup\$ Flagged for conversion to CW (per our convention for 'tips for golfing in x' questions). \$\endgroup\$ – Geobits Sep 11 '14 at 17:32
  • \$\begingroup\$ @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. \$\endgroup\$ – Digital Trauma Sep 11 '14 at 17:41
  • 4
    \$\begingroup\$ @Rainbolt How is this different in nature to other tips for golfing in x questions? \$\endgroup\$ – Digital Trauma Sep 11 '14 at 18:00
5
\$\begingroup\$

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
\$\endgroup\$
  • 5
    \$\begingroup\$ 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. \$\endgroup\$ – Geobits Sep 11 '14 at 17:42
  • 9
    \$\begingroup\$ @Geobits Those are great adjectives for describing Applescript in general. I just haven't decided if Applescript is ridiculously awesome or awesomely ridiculous ;-) \$\endgroup\$ – Digital Trauma Sep 11 '14 at 17:44
4
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ IMO this can be seen as a general tip, not specific to AS. Many modern IDEs do this if you let them. \$\endgroup\$ – Geobits Sep 11 '14 at 17:30
  • 1
    \$\begingroup\$ @Geobits Agreed. Though I think the addition of specific keywords after end is fairly Applescript specific. \$\endgroup\$ – Digital Trauma Sep 11 '14 at 17:31
  • \$\begingroup\$ Yea, that's just baffling to me. The indentation/spacing thing is very common, though, along with unnecessary brackets, etc. \$\endgroup\$ – Geobits Sep 11 '14 at 17:33
  • \$\begingroup\$ @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 \$\endgroup\$ – Digital Trauma Sep 11 '14 at 17:34
4
\$\begingroup\$

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
\$\endgroup\$
  • 3
    \$\begingroup\$ Which itself is tell app "TextEdit" to activate. \$\endgroup\$ – wchargin Sep 12 '14 at 4:16
  • \$\begingroup\$ @WChargin Yes. Add it as another answer if you like. \$\endgroup\$ – Digital Trauma Sep 12 '14 at 18:06
4
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ Haha wow, you must have done some trawling to find this ;) \$\endgroup\$ – Beta Decay Oct 16 '15 at 18:20
  • \$\begingroup\$ I use it a lot. \$\endgroup\$ – Addison Crump Oct 16 '15 at 18:22
  • \$\begingroup\$ The question or the tips? \$\endgroup\$ – Beta Decay Oct 16 '15 at 18:22
  • \$\begingroup\$ The tips. And AppleScript in general. \$\endgroup\$ – Addison Crump Oct 16 '15 at 18:24
  • \$\begingroup\$ Oh I was talking about the question :) \$\endgroup\$ – Beta Decay Oct 16 '15 at 18:24
2
\$\begingroup\$

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"
\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Addison Crump Oct 17 '15 at 21:55
  • \$\begingroup\$ @VTCAKAVSMoACE yes, 's is addressed in this answer codegolf.stackexchange.com/a/37643/11259. Good to know about log" though. \$\endgroup\$ – Digital Trauma Oct 17 '15 at 22:57
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.