Execute applescript without open the editor

12

5

Every time I want to run an applescript the editor pops up.

Is there a way to run it directly?

OscarRyz

Posted 2009-07-29T00:22:22.427

Reputation: 3 691

Answers

19

How the script is saved has a big effect on how it will operate in Mac OS X. It sounds like your script is just saved as a script and that is what is causing the script editor to open every time you open the script.

To solve the problem, open the script in the AppleScript editor and save it as an application. That should to the trick.

The steps are (in the editor)

File > Save As > then set File Format to application then save.

link text

Bruce McLeod

Posted 2009-07-29T00:22:22.427

Reputation: 5 490

2Please note that in Yosemite, the current method is to use File -> Export. There's no more Save As dialogue in the latest version of AppleScript editor. – anon58192932 – 2015-05-22T18:32:29.913

Now it prompts: "Press run to run or quit to quit"... but I guess that's script fault! Thank you – OscarRyz – 2009-07-29T00:38:17.610

1Checking the run check box might stop the prompt from appearing. – Bruce McLeod – 2009-07-29T00:47:54.777

My understanding is that the Run Only checkbox doesn't allow editing of the script later. – jtbandes – 2009-07-29T00:48:30.610

@jtbandes ahhhh ... that makes sense – Bruce McLeod – 2009-07-29T01:01:35.980

*If you ever want to see the script's code again, DO NOT USE Run Only!* The Run/Quit dialog comes from the Startup Screen option. I have sometimes seen it default to checked when using Save As on an already saved application. Also, for Mac OS X prior to Snow Leopard, choose File Format: “application bundle” for maximum compatibility (and minimum resource forks!). Snow Leopard's “Application” is a bundle, they just dropped the “bundle” part of the name (and added x86-64 support in addition to 32-bit Universal support from previous releases). – Chris Johnsen – 2009-10-23T09:06:34.247

7

When saving the script, you can choose "Application" from the File Format dropdown; then you will be able to run it, and you will still be able to drag it to Script Editor to open the script; or you can choose Run Only so it won't save the editable version.

Alternatively, you can use the osascript command in Terminal, either as osascript /path/to/script or osascript -e "a short script here".

jtbandes

Posted 2009-07-29T00:22:22.427

Reputation: 8 350

1

You can also place the script in your ~/Library/Scripts/Finder/ folder and run it directly from the Script menu.

jamiepeloquin

Posted 2009-07-29T00:22:22.427

Reputation:

1

Under macOS High Sierra 10.13 there is no File / Save as.

You must use File / Export / File Format: Application File Export File Format

Jay Walker

Posted 2009-07-29T00:22:22.427

Reputation: 111

0

Another way is to create a Service in Automator which use osascript command to run a .scpt in Finder.

(I am not using Automator in English so the wording may not be accurate)

  1. Launch Automator
  2. File > New, and select Service
  3. In "Service Accepts:" select "File or Folder"
  4. In "Location:" select "Finder.app"
  5. Search "Run AppleScript" and drag the item to the right hand side
  6. In the Run AppleScript box, enter the following code:

    on run {input, parameters}
        tell application "Finder"
            --get the selected file
            set selectedItem to (item 1 of (get selection))
    
            --get location info (folder:file format)
            set fileLocation to (selectedItem as alias) as string
    
            --replace : with / with subroutine
            set the semifinal to my replace_chars(fileLocation, ":", "/")
    
            --remove Macintosh HD with subroutine
            set the theFinal to my replace_chars(semifinal, "Macintosh HD", "")
        end tell
        do shell script "osascript " & "\"" & theFinal & "\""
        return input
    end run
    
    on replace_chars(this_text, search_string, replacement_string)
        set AppleScript's text item delimiters to the search_string
        set the item_list to every text item of this_text
        set AppleScript's text item delimiters to the replacement_string
        set this_text to the item_list as string
        set AppleScript's text item delimiters to ""
        return this_text
    end replace_chars
    
  7. File > Save, and give it a name like "Run AppleScript"

  8. Now you can right-click a .scpt file in Finder and select "Run AppleScript" and see your script executed.

Reference: Source of subroutine - AppleScript: Essential Sub-Routines

Jack Wu

Posted 2009-07-29T00:22:22.427

Reputation: 1

I see issues with your AppleScript code. 1. You have no error handling or code to validate what's selected is a .scpt file before it's passed to osascript and have not coded it to handle if more then one .scpt file is selected in Finder. 2. There is absolutely no need to go through such coding rigmarole when the following single line of code replaces everything you added to the Run AppleScript action. do shell script "osascript " & quoted form of POSIX path of item 1 of input Here's a minimal example to replace everything in your current AppleScript code: https://paste.ee/p/XngKA

– user3439894 – 2017-12-07T11:54:20.143

In the minimal example, the if statement block could be expanded with an else clause that displays a message that the selected file was not a .scpt file. That way the user isn't left wondering why nothing happened if they didn't see they accidentally selected the wrong type of file when the service is run. – user3439894 – 2017-12-07T11:54:24.933