On Nov 1, 2010, at 9:01 AM, Jason Lantz wrote: I asked this question before, but was unable to make any of the suggestions work. I think I need provide more detailed information, so I am trying again.
Objectives: 1. To delete a specific hidden file 2. For the script to work on any machine (aka absent of hard coded file paths) 3. To delete the file without requiring the user to directly choose or select the file
Here's my original script (which works):
tell application "Finder" try exists file "Leopard HD:Library:Application Support:.syssettings12a239c8044f4" if true then set trialcounter to "Leopard HD:Library:Application Support:.syssettings12a239c8044f4" end try
delete trialcounter end tell
Notice that despite the fact that the existence of the hidden file comes back false, the script proceeds anyway and somehow manages to delete the file. Unfortunately, this script isn't sufficient for me because the file paths are hard coded. To fix this I tried the following script.
tell application "Finder" try exists file "Library:Application Support:.syssettings12a239c8044f4" of the startup disk if true then set trialcounter to "Library:Application Support:.syssettings12a239c8044f4" of the startup disk end try
delete trialcounter end tell
This is essentially the same as the first script except that I removed "Leopard HD" from the file path and replaced it with "of the startup disk" at the end. This script returns a syntax error that says "Can't get 'Library:Application Support:.syssettings12a239c8044f4' of the startup disk. Access not allowed."
I feel like the solution here is probably quite simple, but I can't figure it out. What am I doing wrong?
There are a few different problems here:
1. "exists" does not throw an error if the specified item does not exist, it merely returns false. You probably knew that already, but...
2. "if true then" always executes the "then" part. An "if" statement executes its "then" part if the condition evaluates to true, which "true" always does (duh), so there you are. What you probably wanted was "if the result is true", or you could put the "exists" directly into the test, as in 'if exists file "..." then ...'.
3. Given the two bits above, there's no real need to put the "exists" and "if" in a "try" block. (It is still possible for "exists" to fail, but only for more catastrophic reasons like it can't send a command to Finder.)
4. Your control flow, presuming that you're not relying on "trialcounter" being set outside the scope of this fragment, is odd. You're conditionally setting "trialcounter" (or attempting to, anyway), but then always using it in the "delete" statement, which means you'll get an undefined-variable error if the file doesn't exist. What you want is something more like this:
if exists ... set trialcounter ... delete trialcounter end if
Or you could rely on "delete" throwing an error if the item doesn't exist:
try set trialcounter ... delete trialcounter end try
5. Finder is weirdly fussy when it comes to invisible items. It won't admit to an invisible "file" existing, but it will if you refer to it as an "item". For example:
tell application "Finder" exists file "Leopard HD:Library:Application Support:.syssettings12a239c8044f4" --> false exists item "Leopard HD:Library:Application Support:.syssettings12a239c8044f4" --> true end
That (mostly) covers the first script. Now for the second one:
5. Strings are not file references. What you actually needed in that "set" statement is 'set trialcounter to file "..." of the startup disk'.
Summing all that up, here's one version that should work:
tell application "Finder" if exists file "Library:Application Support:.syssettings12a239c8044f4" of the startup disk set trialcounter to "Library:Application Support:.syssettings12a239c8044f4" of the startup disk delete trialcounter end if end tell
Or, more compactly:
tell application "Finder" set f to item "Library:Application Support:.syssettings12a239c8044f4" of the startup disk if exists f then delete f end tell
Or even more compactly:
try tell application "Finder" to delete item "Library:Application Support:.syssettings12a239c8044f4" of the startup disk end try
Furthermore, AppleScript can tell you where the "Application Support" folder is, so you don't need to hard-code that part either:
set ap to path to application support folder tell application "Finder" set f to item ".syssettings12a239c8044f4" of ap if exists f then delete f end tell
--Chris Nebel AppleScript Engineering
|