Can an Applescript determine if it is running from Script Editor or Script Menu?

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

You can detect the name of AppleScript runtime program from AppleScript.

http://piyocast.com/as/archives/9004

1 Like

Perfect, thank you.

Note that if you copy-paste the code from that page, the apostrophes aren’t valid AppleScript-apostrophes and need to be replaced.

DON’T COPY & PASTE.
Each “★Click Here to Open This Script” link has applescript:// url.
So you have to click each link. DON’T COPY& PASTE.

スクリーンショット 2023-03-30 19.55.59

Then you have to copy-paste in Script Editor instead.

If you set Script Debugger’s preference, Script Debugger respond to applescript:// URL click. NEVER COPY & PASTE from my blog.

I wrote & use special tools not to copy & paste from script list on my blog.
And I wrote special notice to title to avoid copy & paste.

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.

I’m planning to disable copy from Blog…

FWIW, this issue affects this site as well. You may notice a number of posts suggesting how to format code in a post.

Hi lagr.

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. :face_with_raised_eyebrow: 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.

1 Like

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
1 Like

Good luck with that, I have Absolute Enable Right Click & Copy – Hämta denna utökning för 🦊 Firefox (sv-SE)

(Though it breaks Facebook (?) so I usually have it disabled)

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

Do you think you can elaborate how to do it?

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.

set text item delimiters to ":"