Why does this simple apple script generate an error?

Joined
Nov 20, 2012
Messages
72
Reaction score
0
Points
6
I named the script below ToggleShowHiddenFiles.scpt. When I run the script I get this error:

./ToggleShowHiddenFiles.scpt
./ToggleShowHiddenFiles.scpt:134:140: script error: Expected “then”, etc. but found identifier. (-2741)


The syntax appears correct, can anyone tell me how to avoid this error?



#!/usr/bin/osascript


tell current application
set HiddenStatus to do shell script "defaults read com.apple.finder AppleShowAllFiles"
if Hidden Status = "NO" then
do shell script "defaults write com.apple.finder AppleShowAllFiles YES && killall Finder"
else
do shell script "defaults write com.apple.finder AppleShowAllFiles NO && killall Finder"

end if
end tell
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
Firstly your syntax is incorrect.
You have written "set HiddenStatus to ect.", then you test for "if Hidden Status = ect.", so the variable spelling is not the same.

Also you do not need the "tell current application" "end tell" block to run a do shell script command.

It's also worth stating what version of OS X and AppleScript you are using, as the "AppleShowAllFiles" key is not valid on my system, which means your using a different version to myself and others who may be wanting to help you.
This OSX version declaration is also important, as the AppleScript language has gone through various changes since OSX 10.6, although these changes are mostly to do with the AppleScriptObjC language used in Xcode, it still may be relevant to someone trying to help with your code.

Regards Mark
 
Joined
Feb 14, 2004
Messages
4,781
Reaction score
166
Points
63
Location
Groves, Texas
Firstly your syntax is incorrect.
You have written "set HiddenStatus to ect.", then you test for "if Hidden Status = ect.", so the variable spelling is not the same.

Where is that? He is getting the result of a defaults read and comparing it. If it's no then he's changing it to yes. I see no ect anywhere in the script.
To the OP: try putting the Then on a separate line? I don't think Applescript is that syntax picky but you never know.
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
@ cradom

Read the AppleScript code posted.

The poster is setting a variable called "HiddenStatus" to the result of the defaults read command.
And then checking with an if statement a variable called "Hidden Status", which has a space in it.
That is incorrect syntax.
You have to check the variable in the if statement with the exact same spelling and case, and with no spaces in it.
The ect. I've used in reply, is for me to eliminate duplicating the correct code following the mistake, although I've typed it wrong, it should be etc.

Mark
 
Last edited:
Joined
Feb 14, 2004
Messages
4,781
Reaction score
166
Points
63
Location
Groves, Texas
Ah, ok. Was wondering about the ect part which was not in the code. Totally missed the space.
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
There are also potentially further errors in the AppleScript, which I would of come to later had the poster replied.

Namley that when reading defaults through a shell command, boolean values are returned as integers of 0 and 1, which then have to be checked for, and converted to AppleScript true or false booleans, or converted to Yes or No string values, whichever would suit any other code in the script.

Mark
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top