I’m not even sure if the title of that is accurate to what I want to do - it just sounded close to what I wanted!
Basically, I’m making a simple script that I will save as an application bundle. The “main” script will prompt the user with a list of available options. Based on the choice, a specific script contained in the scripts folder of the app is ran. This is all working great, however within these other scripts, I want to open text files that are in the Resource folder of the “main” script.
Basically here is the meat of the matter – the “main script”:
if theChoice as text is "Run Script 1" then
run script file ((path to me as text) & "Contents:Resources:Scripts:Script1.scpt")
Then in script 1 would be something like:
...
open file (path to PARENT??? as text) & "Contents:Resources:Text1.txt"
...
The only other option I could think of would be to have every script inside the main script be a bundled script… but that would get to be a very complicated file tree, and I would like to avoid that!
Is there any way to open a file from a folder in a heirachy above the script?
Your approach might work, but you would have to spell parent properly. If usually would have declared a property holding the path in the parent script-bundle or applet , that property would then be visible from within the script you wanted to run.
property theTextFile : (path to me as text) & "Contents:Resources:Text1.txt"
run script file ((path to me as text) & "Contents:Resources:Scripts:Script1.scpt")
An alternative would be to have a handler within the script to run which takes a parameter, (not the run handler), and call that handler with a parameter after you have loaded your script.
I haven’t tried with bundled scripts, but in other situations, you can pass the “parent” script’s path to the other as a parameter:
set resourcePath to (path to me as text) & "Contents:Resources:"
if theChoice as text is "Run Script 1" then
run script file (resourcePath & "Scripts:Script1.scpt") with parameters {resourcePath}
Script 1:
on run {resourcePath}
...
open file (resourcePath & "Text1.txt")
...
end run
Alternatively, if you ‘load’ a script into another before running it, the host script becomes the loaded script’s parent in an AppleScript sense and the loaded script inherits its properties:
if theChoice as text is "Run Script 1" then
run (load script file ((path to me as text) & "Contents:Resources:Scripts:Script1.scpt"))
Script 1:
...
open file (path to me as text) & "Contents:Resources:Text1.txt"
...
Nigel, thank you very much! Your first example was ideal- Passing the parameter like that to the other scripts in the bundle works great! This also solves the problem I was having of passing variables between scripts!
Hello.
Actually I did create a script bundle and stuffed it with the scripts, script1 would be called and would run fine until it tried to access the file.
I tried to run both examples from the script editor and from the script menu. An no one of them seemed to work from within a script bundle.
@huskr88. Are you saying that you ran scripts as script bundles and that they worked.
It’s working OK for me too now I come to test it, McUsr, both with the “parent” script as a script bundle and as an application. However, Script 1 does have to be modified to tell an application to open the text file. It can’t just do it by itself.