Errors when trying to 'get resource' on a newly 'touched' file

Dear MacScripters,

I’m getting a puzzling error in an AppleScript application when it is first started and has to generate some items in the resource bundle. I’m running some UNIX shell scripts and store them in script resources. The script generates the scripts on the fly when first starting.

To make things simple (I thought) I have a handler called by the run handler that creates the folders and using UNIX touch to create empty file locations that are then filled out later. This allows me to use (path to resource . . .) in a uniform way no matter what state the script is in.

What is happening to me is that when the application is run the first time generates a “resource not found” error when the resources (empty files) do in fact exist. The effect is summarized in this code snippet:


-- Test Script to illustrate problem with getting "empty" resources

-- First Create folder and empty files that will later be filled.
set resourcePathStr to (path to me as string) & "Contents:Resources"
set resourcePath to POSIX path of resourcePathStr

-- Create Shell Scripts folder
set shellScriptFolder to resourcePath & "/Shell Scripts"
do shell script ("mkdir '" & shellScriptFolder & "'")

-- Create empty files for the bash and SED scripts.
set bashSEDscript to shellScriptFolder & "/SED_tag_processor.sh"
do shell script ("touch '" & bashSEDscript & "'")
set sedCommandFile to shellScriptFolder & "/LWC_AppleScript_tags.sed"
do shell script ("touch '" & sedCommandFile & "'")


-- These calls generate "resource not found" errors in larger script
set bashSEDscript to POSIX path of (path to resource "SED_tag_processor.sh" in directory "Shell Scripts")
set sedCommandFile to POSIX path of (path to resource "LWC_AppleScript_tags.sed" in directory "Shell Scripts")

If you run this script as a script bundle - it works just fine. However, if you save it as an application - it will generate the “resource not found error” I’m running into.

I’ve worked around the problem by getting the path in the same way I use to touch the file in the first place, but it is annoying. Am I doing something silly? Is there a “more better” way to deal with bundle resources that will exist but may not exist when the application is first run?

Thanks in advance for your help! :slight_smile:

Cheers, Edouard

Model: MacBook Pro
AppleScript: 2.1.2
Browser: Firefox 10.0
Operating System: Mac OS X (10.6)

It seems ‘path to resource’ is too fast for the system to keep up :rolleyes:
The script in post #2 uses the same workaround.

Another way might be to use a delay in a loop, but there’s not much to gain there.

Dear alastor933 and MacScripters,

Thanks for the suggestions and the diagnosis.

Unfortunately, I don’t think a mere delay will help the matter. I tried the obvious delay 1, but that had no effect. More significantly, I put in some dialog boxes to pause the process and no waiting seemed to be long enough. Perhaps the system will update eventually but the delay would become really annoying for a user.

So I use a try block to get the path and if it fails, I reconstruct the path using the script’s location instead. It works.

Certainly not elegant, but sadly scripting has always been a “dirty business” :rolleyes:

Thanks again for the help

Cheers, Edouard :slight_smile:

Actually this is not a work around. Specifying folders within the bundle with path to me is a normal and reliable method.

Why not just checking for the shell script folder?
If it doesn’t exist perform the first run


property shellScriptFolderName : "Shell Scripts"

-- First Create folder and empty files that will later be filled.
set resourcePath to POSIX path of (path to me) & "Contents/Resources/"
set shellScriptFolder to resourcePath & shellScriptFolderName & "/"
set bashSEDscript to shellScriptFolder & "SED_tag_processor.sh"
set sedCommandFile to shellScriptFolder & "LWC_AppleScript_tags.sed"

set firstRun to (do shell script "/bin/ls " & quoted form of resourcePathStr) does not contain shellScriptFolderName
if firstRun then
	-- Create Shell Scripts folder
	do shell script "/bin/mkdir " & quoted form of shellScriptFolder
	
	-- Create empty files for the bash and SED scripts.
	do shell script "/usr/bin/touch " & quoted form of bashSEDscript
	do shell script "/usr/bin/touch " & quoted form of sedCommandFile
end if