Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In AppleScript Editor i can see that source_code has script content but row 2 fails for some reason.

set source_code to load script (POSIX file "/path/to/script.scpt")
word 2 of source_code

This don't work. I try to read AppleScript files to variable.

I don't try to run this loaded code but i need to get it as string.

It seems to fail because source_code is «script» not string. How this can be fixed? Thanks

set source_code to load script (POSIX file "/path/to/script.scpt")
word 2 of (source_code as string) -->error "Can’t make «script» into type string." number -1700 from «script» to string
word 2 of source_code -->error "Can’t get word 2." number -1728 from word 2
share|improve this question
So the first line works (set source_code ...) but the second doesn't? What do you expect the second line to do exactly? – patrix Apr 22 '13 at 15:03
I expect to see second word in source code. Thanks – ps22 Apr 22 '13 at 15:24

The load script command loads a script as an object to be run, rather than as text to read or manipulate.

If you want to access the actual text of your script, you can use osadecompile:

do shell script "osadecompile ~/Library/Scripts/test.scpt"
word 2 of result

Compiled scripts saved with AppleScript Editor usually have LF line endings in strings but CR line endings in other places. osadecompile converts all line endings to LF. do shell script converts line endings to CR unless you add a without altering line endings specifier.

share|improve this answer
Cool, this works! Thanks Lauri Ranta. Is there any way to get text which is in Description in bottom of AppleScript Editor window? – user48288 Apr 28 '13 at 7:30
You could ask another question about it. The description is stored in the resource fork (cat Untitled.scpt/..namedfork/rsrc / xattr -p Untitled.scpt | xxd -r -p) and it is actually rich text, but I don't know how to convert it to rtf or plain text. – user495470 Apr 28 '13 at 9:34

protected by Community May 2 '13 at 15:52

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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