Running an AppleScript script from within an AppleScript app - applescript

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.

Related

How can i launch bash-script on Mac os with automator-app in same directory?

I searched here and on the net, but didn't really find what I was looking for (I'm still a newbie in bash programming).
I want to start a small bash-script via automator-app.
My bash-script and the automator-app should be in the same directory. The app should just start the script (without parameters). (The script manipulates files in the directory where it is located).
I don't want to copy the script somewhere and have the app somewhere else, both should be in the same directory.
However, Automator does not find the bash script.
How can I specify the path correctly?
In Automator I said:
Run-Shell-Script
myshellscript.sh
myshellscript.sh -> is not found. And I don't want a fixed path. The path of the app (and script) should be my working path.
Can someone help me? That would be great.
Solution found:
property myPath : ""
set myPath to POSIX path of ((path to me as text) & "::") as text
do shell script myPath & "myscript.sh " & myPath

Opening a file with an app using a command line in apple script or an automator app

I want to be able to double click to open a files type (HTML) in a particular app (SeaMonkey composer).
Double clicking the file opens SeaMonkey Browser, but I want it to open in Seamonkey Composer. The only way to do it is with the following command line
seamonkey -editor "filename.html"
So, how can I use apple script or automator to open my html files in composer ?
Save the following script as Application in Script Editor:
on run filesList
repeat with fileRef in filesList
do shell script "seamonkey -editor " & quoted form of POSIX path of fileRef
end repeat
end run
Select View > Show Bundle Contents and give it a custom bundle ID. You can then change the file association as above.
The above assumes the seamonkey command is itself just a launcher; if it's actually the full application (which may be the case as it's obviously not a native Mac app), the middle line'll need tweaked a bit:
do shell script "nohup seamonkey -editor " & quoted form of POSIX path of fileRef & " >/dev/null 2>&1"
That should allow the shell script to exit as soon as the seamonkey process is launched, leaving Seamonkey running until you quit it from its GUI.
If you want to use AppleScript to do this, this simple script should do the job:
set filePath to ((path to documents folder) as text) & "filename.html"
tell application "Seamonkey Composer" to open filePath
I don't have the Seamonkey Composer app to test, but it works with BBEdit.
Note that the open command must have a full path to the file.

Applescript run bash script from same directory

I'm trying to build an AppleScript to launch my shell script.
Path structure is as follows
/Users/ryan/myscript/
applescript.scpt
bash.sh
My AppleScript is as follows:
tell application "Terminal"
set folder_path to path to me
set run_cmd to "/bin/bash " & folder_path & "/bash.sh"
do script run_cmd
activate
end tell
Problem is the 'path to me' is not always returning the correct path. When executed using the Mac cd/dvd autoplay behavior folder_path is equal to:
disk:System:Library:CoreServices:SystemUIServer.app:Contents:XPCServices:com.apple.systemuiserver.scriptrunner.xpc:
Is there is a better way of getting the folder path?
If this Script is in a static location, you can do this:
do shell script "/bin/bash" & POSIX path of (path to current user folder) & "myscript/bash.sh"
Path to me refers to the location of the applescript that is running. So if your script is on a disk then it will reference the location on the disk where the script is saved
if it is expected that the shell script will always exist in a folder called "myscripts" that exists in the current user folder then you could use path to current user folder and build out from there
set user_folder to path to current user folder
set folder_path to quoted form of POSIX path of (("" & user_folder & "myscript"))
tell application "Terminal"
activate
set run_cmd to "/bin/bash " & folder_path & "/bash.sh"
do script run_cmd
end tell
Is there a reason why you have to store the shell script in a separate file? Typically, you would put it inline, within the AppleScript code. As far as I know, the “do shell script” command only operates on text, not on a script at a file path. If you give it a variable that contains a path, it will try to run that path as a command. It won’t run the contents of the file as a command.
Here is an example of an AppleScript that runs an inline shell script and puts the results in TextEdit:
property theShellScript : "#!/bin/bash
echo Hello World"
tell application "TextEdit"
activate
set theScriptResult to do shell script theShellScript
make new document
set the text of document 1 to theScriptResult
end tell
… you can of course replace the above shell script with the contents of your own shell script.
If you do need to keep the script in a separate file, the best way to do that is probably to save your AppleScript as an Application, and put the shell script within the Application bundle. “Path to me” is the path of the application that is running the script — not to the script itself — but if you save your AppleScript as an Application, then it runs its own script, and “path to me” works as you originally expected.
Here is an example of an AppleScript that runs a shell script contained within a file that is stored within its own application bundle:
property theApplicationPath : the path to me as text
property theShellScriptPath : theApplicationPath & "Contents:Resources:Scripts:bash.sh"
tell application "TextEdit"
open alias theShellScriptPath
set theShellScript to the text of document 1
set theScriptResult to do shell script theShellScript
make new document
set the text of document 1 to theScriptResult
end tell
With the above script Copy/Pasted into a new document in AppleScript Editor, hold down the Option key and choose File ▶ Save As, and in the Save dialog box, on the File Format pop up menu, choose “Application” and of course give your application a name and click Save. Then in Finder, navigate to where you Saved your application, and 2-finger tap (or right-click) on your application and choose “Show Package Contents.” That opens your application up as a folder, exposing the file system within. Put your shell script file named “bash.sh” inside the folder “Contents/Resources/Scripts” within your application and then close the window that represents your application.
Now when you run your application from anywhere in the file system, it will still be able to find and run its incorporated shell script.

Dynamically load AppleScripts relative to current script's location

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")

Relative file paths with Applescript

I'm trying to make an Applescript that will open a file on a user's computer without knowing the hard drive or user name, presuming the file is in the same place in the user directory.
tell application "Finder" to open "/Users/jim/Dropbox/Getting Started.pdf" as POSIX file
works great, whereas
tell application "Finder" to open "~/Dropbox/Getting Started.pdf" as POSIX file
fails.
Is there any way to accomplish this simply?
You can't use tilde paths in AppleScript basically because POSIX file is in fact an URL. URLs for file paths doesn't support incremental paths, only absolute paths. But the meaning of the tilde in POSIX paths is not something special, it's just replaced by the home folder. SO to get the same results we only need to change your code to:
tell application "Finder" to open (POSIX path of (path to home folder)) & "Dropbox/Getting Started.pdf" as POSIX file
To accomplish this, you could use a shell script instead of tell application "Finder", or you could use a shell script to get the home folder and insert it into your tell block.
To use a shell script, you can use the following code: do shell script "open ~/Dropbox/Getting\\ Started.pdf. To insert a shell script into your Finder tell block, you could use this code: tell application Finder to open (do shell script "echo $HOME") & "/Dropbox/Getting Started.pdf". This uses a shell script to print the path to the logged in user's home directory and uses it in the path you give Finder.
I hope these suggestions help you solve your problem! ✌️ 🇺🇦

Resources