When I run a script from Script Editor I need to set up an application in a certain state before the script executes its main function. When run from the Script Menu that isn’t necessary. I therefore want to do something like this:
if applescript is run from script menu then
delay 10
else if run from script editor then
delay 0
end if
Is that possible?
(BTW, how do you create new tags in this forum? Script menu doesn’t exist as tag and I seem unable to create it.)
I have that setting enabled but if I want to borrow a few lines of code from you, or someone else, I still have to copy-paste them from the script opened in Script Editor, but now I also have a new window open that I don’t want or need.
These URLs are only useful if you want to execute a remote script “as is”, not when you when you want to use part of it in your own script.
Why do you even use “right single quotation mark” (UTF8 E2 80 99) instead of “apostrophe” (ASCII 27) when you display your scripts? You must manually have changed that character if you copied the script from Script Editor to your webpage.
It looks as if, for the most part, this forum’s software only accepts lower-case tags with hyphens between multiple words. If you type “Script Menu” into the “Search or create…” field in the “optional tags” pop-up when you post, you’re offered “script-menu” and that’s all the software will accept, I’m afraid. You can repeat the process multiple times to get a line of tags separated by commas. I’ve done this for you in your first post above. The tags appear beneath the topic’s subject line.
I have this in my Script Debugger script menu to fix quotes:
set whichWindow to 1
set searchPairs to {}
set the end of searchPairs to {"“", "\""}
set the end of searchPairs to {"”", "\""}
set the end of searchPairs to {"”", "\""}
set the end of searchPairs to {"”", "\""}
set the end of searchPairs to {"‘", "'"}
set the end of searchPairs to {"’", "'"}
tell application "Script Debugger"
set oldSearchString to search string
set oldSearchReplaceString to search replace string
set search ignores case to true
set search matches words to false
set search wraps to true
repeat with thisPair in searchPairs
set {search string, search replace string} to thisPair as list
tell current document of script window whichWindow
set searchResult to search with action replace all
end tell
end repeat
set {search string, search replace string} to {oldSearchString, oldSearchReplaceString} as list
end tell
Here is a plain AppleScript that will get you the results
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set mePath to (path to me) as text
set cp to properties of me
set cp to name of cp
if cp is in {"Script Debugger", "Script Editor"} then
if text -1 thru -5 of mePath = ".app:" then -- is app running as script
set fileLoc to (text items 1 thru -3 of mePath) as text
display alert "is app running as script!" & return & cp
else -- is running as script
set fileLoc to (text items 1 thru -2 of mePath) as text
display alert "is running as script!" & return & cp
end if
else -- is running as standalone app
set fileLoc to (text items 1 thru -3 of mePath) as text
display alert "is running as standalone app!" & return & cp
end if
Are you sure? I tried some single word tags now, and nothing happens when I click them or press enter. See this screen recording https://imgur.com/a/akaUUUj
Thanks for this! I have it mostly working in that it can correctly detect a script and an app running as a script, but for stand alone applications I get the error Can’t get «class pALL». (-1728) on the properties of me line. Any thoughts on why that might be and how to fix it? Right now I’m just catching the error and assuming that means it’s a stand alone app.
I made a simple script that gets the properties of an app (eg firefox) and there were four: frontmost, class, name, version.
I then had the script app above attempt to get each property individually from itself. It works with frontmost, name, and version, but not with class — hence the error when getting all properties.
This script below gets the current application to determine what is running the script and then separately, the name of the document or .app. It avoids the issue you’re experiencing by getting only the name property, which is available in all three situations.
set mePath to (path to me) as text
set cp to name of me -- will be name of standalone app or of document
set ca to name of current application -- will be name of standalone app or editor
if ca is in {"Script Debugger", "Script Editor"} then -- check is for name of current application, not of document
As an aside, I think that for the various fileLoc lines to work, the text item delimiters should first be set to “:”. It will only make a difference when run from the editor but I thought it should be noted.