run applescript from terminal code example

Example: run an applescript

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

Tags:

Misc Example