The Computer Oracle

Execute AppleScript without opening the editor

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2 Looping

--

Chapters
00:00 Execute Applescript Without Opening The Editor
00:11 Accepted Answer Score 27
00:42 Answer 2 Score 14
01:06 Answer 3 Score 1
01:15 Answer 4 Score 0
02:28 Thank you

--

Full question
https://superuser.com/questions/14762/ex...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#macos #applescript

#avk47



ACCEPTED ANSWER

Score 27


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




ANSWER 2

Score 14


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




ANSWER 3

Score 1


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




ANSWER 4

Score 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