r/applescript • u/LawrenceSan • Nov 23 '22
Can't edit my AppleScript
I have an old script that I wrote years ago (.scpt file) that worked fine until recently but now is screwing up. It's a complicated script, involving (among other things) my Mac's hosts file and a few different applications… and I think my Mac's setup has changed in some way. I guess I neglected to save it as a plain-text file, I usually don't bother because I normally can open any .scpt file in Script Editor for editing, with no problem.
There's something weird about this script, however. No matter how I try to open it in Script Editor, or the older AppleScript Editor, or Smile… the script runs instead. (And screws up big-time, including freezing whatever script editor I tried to open it in, so I have to force-quit the script editor.)
I've never encountered anything like this before. Why does it refuse to open in an editor? Why does it always run instead?
I can open it in a plain-text editor (like Text Editor or BBEdit) but then it looks like mostly gibberish with just a little English. In fact most of the characters look like Chinese or something! I tried different encodings in BBEdit (UTF-8, UTF-16, etc) but that didn't help. If I just copy & paste the gibberish into Script Editor, it can't make any sense of it either.
I found something to try using command line in the Mac's Terminal: osadecompile … and its man file (instructions) seem dead simple… but even trying that ran the script instead of decompiling it!
What could possibly cause a script to always run when you tell a script editor to open it? Any idea what I can do? Thank you.
View all comments
Show parent comment
1
u/ChristoferK Nov 28 '22 edited Nov 28 '22
My apologies, I interpreted your previous comments to mean that you'd been limited to working on old machines, so weren't likely to be familiar with the posix standards. Sorry if that all came across as patronising.
Posix-style paths can be utilised in AppleScript by one of two methods:
POSIX filetyoe specifier: this enables the construction of a file reference by pairing thePOSIX fileterm (which exists as both a specifier as well as a type class) with the posix-formatted path, e.g.This returns a
filereference object that will show the path using an HFS format, and is mostly for when you want to work with Finder, which doesn't understand posix paths.The
POSIX filespecifier doesn't expand the tilde, so it needs to be given an absolute path.However, you're instinct to use System Events for file-related tasks is a good one:
2. System Events: System Events does understand posix paths, including the tilde notation. So you can use this freely. Often, if you're wanting to do something with a file or folder, then you need to pair the posix path string with a
fileorfolderspecifier, which needn't be given an absolute path, as you can reference afileorfolderobject relative to its containing parentfolder.The reason you encountered an error when trying to obtain the builtin reference to the utiliities folder is because you passed it a string. The list I offered are a list of properties belonging to System Events:
This returns a folder reference, but of course its a folder reference that belongs to System Events, so Finder won't know what to do with it (which is the actual cause of the error, rather than the incorrect invoking of the
utilities folderproperty).Your choices from this point would be to stick with System Events, as this will automatically invoke Finder to open folders anyway:
or, if you need to pass file references from System Events to be utilised by Finder, then make sure to process them so you provide Finder the paths in a form it understands. Finder understands precisely three forms: a plain string path in HFS-style format, or an
aliasfile reference object, or aPOSIX filefile reference. Here's each as worked examples, firstly by passing through a simple string HFS path:and secondly by coercion to an
aliasclass reference:and finally by way of
POSIX file:(1)utilised thePOSIX filespecifier, and requires themykeyword because thePOSIX fileterm is owned by the AppleScript instance and not Finder. You can utilise it withoutmyif done so outside the Finder block, which I chose not to specifically to exemplify this point.(2)utilised thePOSIX filetype class to perform a type coercion. In practice, this is the simplest way as it's easy to forget about what owns what and when, whereas a coercion performed on an explicit value (like a string) will always work. Coercions are also more expensive operations, but this isn't going to be a problem in practice.If any of this is confusing, let me know. Personally, I avoid Finder as much as possible. System Events is faster and simpler, but there are one or two things it cannot do that necessitate the use of Finder. But note from the first of the above three examples that it wasn't necessary to make a file reference at all, as Finder was able to take a plain string path and act upon it appropriately. The same is true of System Events (but to a slightly lesser degree because of a bug), and in both cases, only applies when using string paths as parameters in a command. To retrieve properties for a file, this requires an appropriate reference object rather than just a plain string path.