I’m trying to write a build script that uses sh scripting and AppleScript mixed together and I’m having some trouble - I think with aliases.
The .sh script pretty much looks like this (pseudocode):
#get the args, do some other stuff
if [ clean ]
rm -rf $BUILD_DIR
fi
if [ dir doesn't exist ]
mkdir $BUILD_DIR
fi
#ok, now execute an old applescript to copy some data to the build dir
osascript CopyData.script
#do the build and more stuff....
The AppleScript looks like this:
tell application "Finder"
set BuildDir to folder "BuildDir" of startup disk
set SourceDir to folder "SourceDir" of startup disk
duplicate file "file1.dat" of SourDir to BuildDir with replacing
duplicate file "file2.dat" of SourDir to BuildDir with replacing
end tell
The problem is that when the .sh script executes the osascript line, it returns this error:
execution error: Finder got an error: The operation could not be completed because the item could not be found. (-120)
It seems to me that the AppleScript is somehow saving an alias to the folder which becomes invalid after I delete the directory. Is this really what’s happening? Is there better AppleScript that will always work? Does anyone know exactly how AppleScript deals with files, aliases, etc.? I’ve decided that the faster way for now is just to rewrite the AppleScript as a shell script, but I’m curious about whether there is actually a way to make this work. I’m pretty much an AppleScript newbie, so I hope this isn’t a stupid question.
If folders and files are there, shouldn’t be problems. What’s going on in the shell script before osascript is invoked?
However, if you are simply dealing with files (remove, create, duplicate), I wouldn’t mix shell-applescript, since it is not needed (you can do it with a single technology).
The only semi-persistent file reference in AppleScript is “alias”:
display dialog (read alias "path:to:file.txt")
At compile-time the file must exist. If (after you saved the script) you move “file.txt” to a different location or rename it, the script will still remember what is the file, while it is not deleted.
Other file references will not persist. For instance, for your code to work, the Finder needs a file called “file1.dat” within a folder called “SourceDir” at your startup disk, and also a folder called “BuildDir”. Otherwise, it will break.
Well, thanks for noticing my typo in my post, but that’s not the problem. The real code of course has the correct SourceDir.
I’ve since converted the whole script to a .sh as was suggested, but I still don’t have a real answer for the problem I was seeing. I can’t really spend work time looking into this, but if I get some free time, I’ll try to play with it some more and see if I can figure anything out.