I would like to be able to reference a model file that is located in the same directory as the controller.
Initially, they are located in the project's root folder, but when they are compiled (using osacompile) they both will be located in Controller.scptd/Contents/Resources/Scripts directory.
Controller.applescript:
property path : (container of (path to me) as text) <-- Error: Can’t make container of alias "Macintosh HD:Users:craig:Projects:AppleScript:Foobar:Controller.applescript" into type text.
property model : load script file (path & "Model.scpt")
I can't get the syntax working correctly; I've been unable to find a workable solution. Is there a way to get this working?
** edit **
As noted by #dj-bazzie-wazzie and #mklement0, path to me in the context of an application bundle refers to the script bundle (Controller.scptd in my example), not to the script itself (Controller.scptd/Contents/Resources/Scripts/main.scpt).
Assuming that Model.scpt has been bundled with main.scpt in the /Contents/Resources/Scripts directory, this syntax works:
set Model to load script (path to resource "Model.scpt" in directory "Scripts")
Unfortunately, it doesn't work with non-bundled scripts; #Michele-Percich's solution will work, however.
Is there a single syntax that work work in both situations?
Properties are initialized (set) at compile time and their values are persistent. So even if you have dynamic values in there, the script will only keep the value of the command/class when it the script was compiled for the last time. Here an example of how properties work:
property a : current date
return a
You can keep clicking the run button in AppleScript-Editor but you'll see that the date isn't updated. This is because when the script compiles, the compiler noticed current date and uses its value. It's not a reference to current date that is stored into the property, but the value returned by current date at compile time, which is just a date value.
You just need to load the script every time when the script is launched.
property model : missing value
set model to load script file ((path to me as string) & "Contents:Resources:Scripts:Model.scpt")
EDIT: Update my answer after the question's has beeing updated
If you want to make use of libraries inside and outside library bundles I would take a look at script libraries, if you're running Mavericks. When you're using the use statement to load a script library and the running script is a bundle (saved as application or script bundle) it will first tries to load the library from there. If fails or the script is just an file and not a bundle it will load the script library from the 4 library folders (user, computer, network, system library and in that order).
I have written a tutorial about how to write script libraries
To address the requirement added later that the code should work in both the following scenarios:
When running as simple *.scpt file: load the other script from same folder (as the running script itself)
When running as *.scptd bundle: load the other script from the bundle (technically, the same folder as the main script inside the bundle).
This solution builds on the helpful answers provided by #Michele Percich and #dj bazzie wazzie; #dj bazzie wazzie's great tutorial on script libraries is well worth a read.
set otherScript to "Model.scpt"
if (path to me as string) ends with ":" then # Running as bundle?
set otherScript to ¬
(path to resource otherScript in directory "Scripts") as string
else
tell application "Finder" to set otherScript to ¬
(container of (path to me) as string) & otherScript
end if
set model to load script file otherScript
path to me as stringending in : implies that the path is a folder. Since path to me returns the bundle folder when running as a script bundle (AppleScript-based *.app bundles do the same), we can deduce that the running code is part of a bundle.
container is a Finder's element.
Also, why are you using properties?
Anyway, this should work:
tell application "Finder"
set myPath to container of (path to me) as text
end tell
set model to load script file (myPath & "Model.scpt")
Related
I am having a problem defining a relative path to an HTML file from within an Automator workflow.
Problem:
The file structure is as follows:
portable folder
Automator.app
Chromium.app
assets folder
-- HTML file (my-file.html)
I have a built a simple app using Automator which launches a new instance of Chromium (portable) and displays a local HTML file when executed. The path to the HTML file is the part I am having problems with.
The first part of the Automator workflow is an Applescript that sets the working directory to the location of the .app file.
on run {input, parameters}
set p to path to me
return p
end run
The second part of the Automator workflow is a shell script that is supposed to open the HTML file in the provided Chromium browser.
APP_PATH=$1
cd "$APP_PATH"
# open chromium from current directory
open -n -a Chromium.app --args --user-data-dir='/tmp/chrome_dev_test' --allow-file-access-from-files --allow-file-access --allow-running-insecure-content '/assets/my-file.html'
The browser opens as expected when my Automator app is double clicked and it attempts to open the HTML file but it can not find it. The address bar displays the path to the HTML file I have instructed it to open, so it is trying. The HTML file displays fine when dragged and dropped into the browser (no problems with the included --args).
I am not sure what to prepend to my file location in order for it to open the HTML file properly. With the cd "$APP_PATH" instruction, I believe I have set the working directory to be "portable folder" so the "/assets/my-file.html" would be the relative path to the file I want to open.
The Automator app and the assets folder that holds the HTML page will always be in the same folder so their relationship will always be the same. The entire folder containing all necessary files is meant to be portable so it does not have a defined location on a users system. It can be run off a USB key or the Desktop or wherever.
How do I format the URL in my shell script to recognize the full path to the HTML file given that it is portable but will always have the same location with respect to the "$APP_PATH" ?
First of all p is an AppleScript alias specifier, you need a POSIX oath.
Second of all p points to the app itself but you need the reference to the parent folder.
System Events gives you the POSIX path of the parent folder. Append the folder name and the name of the HTML file
on run {input, parameters}
set p to path to me
tell application "System Events" to set parentFolder to POSIX path of container of p
return parentFolder & "/assets folder/my-file.html"
end run
assets folder represents the name of the folder
I'm implementing this code into an AppleScript app I made the other day, but it doesn't seem to work. I'm trying to run another AppleScript script.
set filescript to (path to resource "file creator.scpt" in directory "Scripts")
run script (path of filescript)
The error said that It can't get a class PPTH of alias.
Here's how I do it currently:
run script alias (((path to home folder) as string) & "projects:Another Script.applescript")
So in this example, the other script that I want to run is in ~/projects/Another Script.applescript. I use this to run another applescript within a script saved as text, or application.
I'm making a small script that needs to use the name of the parent directory of the script itself (.scpt file) a variable.
For example, the script is located at /Users/spongebob/MyProject/myscript.scpt
I need to set the variable called myprojectdir to MyProject.
I've tried
set myprojectdir to parent of POSIX path of me
and other variations of this based on search results but I always end up with an error
Can’t get POSIX path.
Where am I going wrong?
Thanks
AppleScript itself has no idea.
You have to ask System Events
tell application "System Events" to set myprojectdir to name of container of (path to me)
or the Finder
tell application "Finder" to set myprojectdir to name of parent of (path to me)
Here is my code:
tell application "Finder"
move POSIX file "/Volumes/Toggle Desktop Icons/Toggle Desktop Icons/Install Files/Hide Icons.workflow" to folder "~/Library/Services"
end tell
And it always gets this error: Can’t get folder "~/Library/Services".
Any help? This also needs to work on any mac I run the code on. Without changing the code
Thanks.
There is a system property path to home folder that you could use. And for simplicity sake use the native mac HFS path delimiter ":" to reference the rest of your path.
Try this (adding back in your full source path),
tell application "Finder"
move POSIX file "/Volumes/.../Hide Icons.workflow" to folder (((path to home folder) as text) & "Library:Services")
end tell
Try this:
Make a new XCode4 Applescript project. In the delegate, paste this code:
on doIt_(sender)
set goodHFSLoc to (path to desktop folder)
set test1 to (POSIX path of goodHFSLoc)
log "test1:"&test1
set theJSPath to "/Users/dave/Desktop/MakeTSLabels.js"
set jsHFSFile to (POSIX file theJSPath)
set test2 to (POSIX path of jsHFSFile)
end doIt_
Hook this method up to a button in the UI window.
Run the program
Click the button, and you should get this error:
Can’t get POSIX path of class "NSObject".
Put the same code (minus the "on" and "end" lines) into AppleScript editor, and it runs fine.
Apparently, "POSIX file" in ApplescriptObjC doesn't make a file object as the language specification requires. Instead it makes an NSObject.
I need to have an applescript file specifier to provide to Adobe Illustrator's do javascript command, and I need to use NSBundle's functions to get the javascript file, which is packaged in my application bundle.
Am I doing something wrong?
You will see the same behavior in a Finder or System Events tell statement. The solution is the same for ASObjC - you need to use it as a coercion:
set jsHFSFile to (theJSPath as POSIX file)