AppleScript

18 Sep 2022

Need to learn it at some point - can be very helpful for many desktop automations where Python might not be ideal/best suited.

Use Script Editor.app to create scripts.

First working script/snippet, based on copy/paste and basic tweaking:

on run {input, parameters}

    tell application "Finder"
        set filename to name of file input
    end tell

    set the clipboard to filename
end run

➡️ Copy file name to clipboard upon saving file in macOS

27 Sep 2022

open a file in a specific application

tell application "Atom" to open POSIX file "/path/to/file/test.txt"

how to run applescript from command line

osascript /path/to/file/my_script.scpt

date formatting

set date_now to ((current date) as string)

outputs:

"Tuesday, 27 September 2022 at 10:57:26"

string concatenation

set a to "This "
set b to "seems to "
set c to "work."
set myVar to a & b & c

outputs:

"This seems to work."

timestamp

set timestamp to (do shell script "date '+%y%m%d-%H%M'")

Script: write clipboard content to txt file and open in Atom

working:

do shell script "pbpaste > /Users/xxx/my_folder/220927-1121.txt"

tell application "Atom" to open POSIX file "/Users/xxx/my_folder/220927-1121.txt"

need to figure out how to do string injection or variable insertion of output path.

working path construction:

set root_path to "/Users/xxx/my/path/"

set timestamp to (do shell script "date '+%y%m%d-%H%M'")

set file_extension to ".txt"

set file_path to root_path & timestamp & file_extension

outputs:

"/Users/xxx/my/path/220927-1128.txt"

working script 😁

set command to "pbpaste > "

set root_path to "/Users/xxxx/my/folder/"

set timestamp to (do shell script "date '+%y%m%d-%H%M'")

set file_extension to ".txt"

set file_path to root_path & timestamp & file_extension

set full_command to command & root_path & timestamp & file_extension

do shell script full_command

tell application "Atom" to open POSIX file file_path

Key it seems is that variables come out enclosed with quotes which were redundant with the quotes required for the shell script instructions, so including the command itself in the string was the solution.

Update: write clipboard content to txt file and open in CotEditor

21 May 2023

I have started using CotEditor instead of Sublime & Atom before.

CotEditor

Updated script to use Coteditor and working around the initial issue of ""

set command to "pbpaste > "
set root_path to "/Users/xxx/my-notes-folder/"
set timestamp to (do shell script "date '+%y%m%d-%H%M%S'")
set file_extension to ".txt"
set file_path to root_path & timestamp & file_extension
set full_command to command & root_path & timestamp & file_extension

do shell script full_command
do shell script "chmod 644 " & quoted form of file_path

do shell script "open -a CotEditor " & quoted form of file_path

quoted form of

to ensure that strings such as (literal) filenames are passed to the shell unmodified (to protect strings from shell expansions), prefix them with quoted form of.

Opening an application and moving it to a specific screen

20 Apr 2024

tell application "Audirvana Studio"
    activate
end tell

delay 2

tell application "System Events"
    tell process "Audirvana Studio"
        set frontmost to true
        delay 1
        -- Set the initial size to ensure it's manageable
        set size of window 1 to {1024, 600}
        delay 1
        -- Check if the "Move to Elgato Prom." menu item exists and click it if it does
        tell menu bar 1
            tell menu bar item "Window"
                tell menu "Window"
                    if exists (menu item "Move to Elgato Prom.") then
                        click menu item "Move to Elgato Prom."
                        delay 1
                        -- Adjust the window size on the "Elgato Prom." monitor
                        tell menu bar 1
                            tell menu bar item "Window"
                                tell menu "Window"
                                    click menu item "Zoom"
                                end tell
                            end tell
                        end tell
                        delay 1
                    end if
                end tell
            end tell
        end tell
    end tell
end tell

tell application "Audirvana Studio"
    playpause
end tell

Resources

links

social