Tips for golfing in Applescript

13

1

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

Beta Decay

Posted 2014-09-11T16:52:13.817

Reputation: 21 478

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 – 2014-09-11T16:55:38.910

1Still, it's just a funny question :) – MadTux – 2014-09-11T17:26:53.917

5Flagged for conversion to CW (per our convention for 'tips for golfing in x' questions). – Geobits – 2014-09-11T17:32:18.043

@MadTux Yep, Applescript is not usually the 1st choice in golfing languages ;-). But there are occasions, particularly in the [tag:restricted-source] category where it becomes marginally useful.

– Digital Trauma – 2014-09-11T17:41:54.370

@Rainbolt Just to be clear, is your comment aimed at this question in particular, or all [tag:tips] for golfing in x questions in general? – Digital Trauma – 2014-09-11T17:47:39.880

@DigitalTrauma This question in particular. – Rainbolt – 2014-09-11T17:59:29.043

4@Rainbolt How is this different in nature to other [tag:tips] for golfing in x questions? – Digital Trauma – 2014-09-11T18:00:58.193

@DigitalTrauma I never said it was. If you wish to continue this interrogation, please open separate chat room and invite me. – Rainbolt – 2014-09-11T18:02:57.357

Answers

6

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

Digital Trauma

Posted 2014-09-11T16:52:13.817

Reputation: 64 644

6It 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 – 2014-09-11T17:42:00.280

9@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 – 2014-09-11T17:44:19.843

4

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.

Digital Trauma

Posted 2014-09-11T16:52:13.817

Reputation: 64 644

IMO this can be seen as a general tip, not specific to AS. Many modern IDEs do this if you let them. – Geobits – 2014-09-11T17:30:05.350

2@Geobits Agreed. Though I think the addition of specific keywords after end is fairly Applescript specific. – Digital Trauma – 2014-09-11T17:31:14.973

Yea, that's just baffling to me. The indentation/spacing thing is very common, though, along with unnecessary brackets, etc. – Geobits – 2014-09-11T17:33:37.950

@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 – 2014-09-11T17:34:59.883

4

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

Digital Trauma

Posted 2014-09-11T16:52:13.817

Reputation: 64 644

3Which itself is tell app "TextEdit" to activate. – wchargin – 2014-09-12T04:16:55.147

@WChargin Yes. Add it as another answer if you like. – Digital Trauma – 2014-09-12T18:06:23.273

4

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.

Peter Hosey

Posted 2014-09-11T16:52:13.817

Reputation: 141

3

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

Addison Crump

Posted 2014-09-11T16:52:13.817

Reputation: 10 763

Haha wow, you must have done some trawling to find this ;) – Beta Decay – 2015-10-16T18:20:44.603

I use it a lot. – Addison Crump – 2015-10-16T18:22:20.717

The question or the tips? – Beta Decay – 2015-10-16T18:22:45.493

The tips. And AppleScript in general. – Addison Crump – 2015-10-16T18:24:07.450

Oh I was talking about the question :) – Beta Decay – 2015-10-16T18:24:44.150

Oh! No, it was one of the things suggested for me in the sidebar. – Addison Crump – 2015-10-16T18:25:19.160

Lots of good tips here - generally you can add one answer per tip. – Digital Trauma – 2015-10-17T23:00:16.197

2

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"

Digital Trauma

Posted 2014-09-11T16:52:13.817

Reputation: 64 644

2You 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 – 2015-10-17T21:55:42.120

@VTCAKAVSMoACE yes, 's is addressed in this answer http://codegolf.stackexchange.com/a/37643/11259. Good to know about log" though.

– Digital Trauma – 2015-10-17T22:57:52.817

2

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.

Addison Crump

Posted 2014-09-11T16:52:13.817

Reputation: 10 763

2

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.

Addison Crump

Posted 2014-09-11T16:52:13.817

Reputation: 10 763

1

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.

kernigh

Posted 2014-09-11T16:52:13.817

Reputation: 2 615