Is there a way to get the path to a script runned by another application?
Obviously path to me returns the application running the script.
I don’t seem to find help in the forum. (MacOs X.3 unfortunately)
Thank you, Orso
Browser: Safari 312.6
Operating System: Mac OS X (10.3.9)
That truly helped me no end. Thank you!
Now I know it is possible.
Please expand…
I don’t exactly need the path to a script runned by another script, I need the path of a script runned as attached script by an app. Concretely, TextWrangler.
set myPath to ((path to library folder as text) & "Application Support:TextWrangler:Scripts:YourScriptName.scpt")
-- or
set tPath to (path to application support folder from user domain as text) & "TextWrangler:Scripts:YourScriptName.scpt"
Thank you for the prompt answer.
I hoped to avoid hard coding it. If the user moves a script into some other folder for ordering the scripts -as typically done in TextWrangler- the path is not valid any longer and I’m bound to ask the user to locate his script. So long, acceptable. I wished there was a trick.
set p to ((path to application support from user domain as text) & ¬
displayed name of (info for (path to me)) & ¬
":Scripts:WhateverUserFolder:whatever.scpt") as alias
works. But only if whateverUserFolder is hard coded.
What surprised me most, is that TW do not offer a listing of the installed scripts with their path.
Orso
P.S.
I use the notation with info for because my script is -should be- valid for both TW and BBedit.
set path_a to POSIX path of (path to application support from user domain as text) & displayed name of (info for (path to me)) & "/Scripts"
set script_name to "yourscriptname.scpt"
set scrip_t to do shell script "find " & quoted form of path_a & " -name " & quoted form of script_name
set Script_path to POSIX file scrip_t as alias
I tested this from script editor by putting a folder in application support for Script Editor with a Folder named Scripts.
inside that I created a untitled folder with the yourscriptname.scpt inside
The above script found it no problem. alias “Macintosh HD:Users:UserName:Library:Application Support:Script Editor:Scripts:untitled folder:yourscriptname.scpt”
But you may want to add some error checking incase your people remove the script completely.
The help of Mark Hunte helped me a lot.
Since this little trick leaves space to multiple interesting uses, I think it is proper to share the complete script on the present forum.
The script below is a concrete example. Thus easier to understand and dissect for other uses.
Save the script below in your TextWrangler/BBedit Script folder: ~/Library/Application Support/TextWrangler/Scripts/ ~/Library/Application Support/BBedit/Scripts/
Download the script Wiki-Previewer.scpt here: http://vcor.net/wiki/index.php/User:Orso. There you’ll also find an introduction to what it does. Short: preview the text contained in a TW/BBedit window on the wiki VCOR (a wiki dedicated to VectorWorks). You can adapt the script for any mediaWiki based wiki. Enjoy it, but use it ONLY if you’re familiar with wikies and their rules. Above all, netiquette rules.
The downloaded script should also be stored in the Scripts folder of TW or BBedit.
If you run the script below from TW or BBedit, it happens:
search for the path of the script Wiki-Previewer.scpt
if this is not found, search for it through the shell
if this fails, ask the user to locate it
clear the cache of Safari
run the second script Wiki-Previewer.scpt
(which previews the content of a TW/BBedit page on a wiki)
I needed it for developing css for the previously mentioned wiki.
The Wiki-Previewer.scpt can opeate alone and is also launchable by TW/BBedit, if its in its Scripts folder.
You can easily adapt the script below to load any script on any position of your HD.
It builds a common launcher from TextWrangler or BBedit. Ideally, you could have only that as interface for launching any appleScript found anywhere on your hard disk.
Orso
-- run the script Wiki-previewer.scpt clearing out the cache of Safari first
-- 1.02 Orso B. Schmid, 3 sept 2006
-- run the script Wiki-previewer.scpt clearing out the cache of Safari first
-- THIS SCRIPT WORKS ONLY IF LAUNCHED BY TEXTWRANGLER OR BBEDIT
-- IT RELYES ON THEYR NAME FOR BUILDING THE APPLICATION SUPPORT FOLDER PATH
-- it is a workaround to run another script -pScriptName-, which serves as library as well as standalone script
-- my thanks to JJ and Mark Hunte for the invaluable help with the shell lines, MacScripter: http://bbs.applescript.net
property pScriptName : "Wiki-previewer.scpt" -- name of the main script, change this if you rename the file!
property pScriptFolder : missing value -- the Scripts folder within the Application Support Folder for BBedit/TextWrangler
property pScriptPath : missing value -- the path to the script to run
-- check if the pScriptPath is still valid
try
pScriptPath as alias
on error
set pScriptPath to missing value
end try
if pScriptPath is missing value then
display dialog "The script " & pScriptName & " is not in the correct location. I'll try to find it."
try
-- identify TextWrangler/BBedit Scripts folder in user domain
set pScriptFolder to (path to application support from user domain as text) & displayed name of (info for (path to me)) & ":Scripts:"
-- returns one of the following paths (in mac notation):
-- ~/Library/Application Support/TextWrangler/Scripts/
-- ~/Library/Application Support/BBedit/Scripts/
-- path to me here is not the path to the present script
-- since TextWrangler/BBedit are launching the script, the path refers to them
-- search for it in the Scripts folder
-- My thanks to Mark Hunte for the shell line, http://bbs.applescript.net/viewtopic.php?id=18471
set tempP to do shell script "find " & quoted form of (POSIX path of pScriptFolder) & " -name " & quoted form of pScriptName
set tempP to POSIX file tempP as alias -- if this fails, error
set pScriptPath to tempP -- if error, this doesn't happen
on error
-- the user must have renamed or removed the script
display dialog pScriptFolder as alias as text
set tempC to choose file of type {"osas"} default location (pScriptFolder as alias) with prompt "The script " & pScriptName & " has been renamed or removed. Please locate it:"
-- and now hope the user picked the right one
set pScriptPath to tempC
end try
set pScriptName to name of (info for pScriptPath)
tell application "Finder" to set pScriptFolder to (container of pScriptPath) as alias -- rewrites the path to it
display dialog "The new location for the script is: " & return & return & pScriptPath
end if
--> empty cache files
try
-- if the cache is already empty: error
--My thanks to JJ for the shell line, http://bbs.applescript.net/viewtopic.php?id=12155
do shell script "cd ~/Library/Caches/Safari; rm -r *" -- clear Safari cache
on error
display dialog "The cache was already empty."
end try
run script pScriptPath
Just a thought but do you need both (path to application support from user domain as text) & displayed name of (info for (path to me)) & ":Scripts:
how about doing it like the extract below
property pScriptName : "Wiki-previewer.scpt" -- name of the main script, change this if you rename the file!
property pScriptFolder : (path to application support from user domain as text) & displayed name of (info for (path to me)) & ":Scripts:" -- the Scripts folder within the Application Support Folder for BBedit/TextWrangler
property pScriptPath : pScriptFolder & pScriptName -- the path to the script to run
-- check if the pScriptPath is still valid
try
pScriptPath as alias
on error
set pScriptPath to missing value
end try
if pScriptPath is missing value then
display dialog "The script " & pScriptName & " is not in the correct location. I'll try to find it."
try
-- identify TextWrangler/BBedit Scripts folder in user domain
--set pScriptFolder to (path to application support from user domain as text) & displayed name of (info for (path to me)) & ":Scripts:"
-- returns one of the following paths (in mac notation):
-- ~/Library/Application Support/TextWrangler/Scripts/
-- ~/Library/Application Support/BBedit/Scripts/
-- path to me here is not the path to the present script
-- since TextWrangler/BBedit are launching the script, the path refers to them
Because since it is a property as soon as I work on the script wtih Debugger it gets loaden… and not with a missing value. The error catching should be different, your suggestion might make the script more efficient.
Did ever a script finish?