Contacts – Automate the Numbers spreadsheet using AppleScript

This is a bit long, but here it goes …

With AppleScript (vanilla, please), I want to automate the Numbers spreadsheet I made for my wife's music lesson accounts (which she uses four times a year, she is the only user).

This is my first AppleScript project. I have found some individual bits, which work partially, and I would like help getting them together.

Script 1A and script 1B must be combined into ONE script, but I don't know how to do it.

Script 1 should only be executed once, after it (manually) opens the spreadsheet, but script 2 will be executed for each student's account, to generate an email (approximately 25 individual times)

Script 2 needs to get information from Script 1 – see below.

The general sequence should be like this:

1) The user makes sure that the contacts are updated on the laptop (Catalina) and manually opens the Accounts spreadsheet.
2) Run scripts 1A and 1B (which will be ONE script)
3) Complete the details
4) Run script 2
5) Verify and (manually) send an email.
6) Repeat from 3) for each student

1A script

--returns Contacts group’s names and email addresses

set myList to ""
tell application "Contacts" to repeat with p in people in group "testGroup"
    if emails of p is {} then
        set e to ""
        set e to value of email 1 of p
    end if
    set myList to myList & name of p & ":" & e & linefeed -- gives full names
end repeat

(The result of this is, for example, …) (I don't know if this result is in a proper format …)

"Peter Pan:
Joe Bloggs:
Molly Mouse:

1B script YOU MUST USE the elements of the previous result in Script 1A, NOT in the list of menu items below!

-- Make Pop-Up Menu in Numbers spreadsheet which is already open.
set menuItems to {"Joe Bloggs", "Molly Mouse", "Peter Pan"}

tell application "Numbers"
    tell the first table of the active sheet of document 1
        tell cell "A3"
            -- Set the cell value to the first menu item required. This also selects the cell.
            set value to item 1 of menuItems
            -- THEN set the cell format. This causes the menu to adopt the value just set as a single item instead of having the three default items.
            set the format to pop up menu
        end tell
    end tell
end tell

tell application "System Events"
    tell application process "Numbers"
        set frontmost to true
        tell window 1
            -- The inspector doesn't have to be visible, but its "Cell" pane does have to be selected. (?)
            click radio button "Cell" of radio group 1
            -- Adds the remaining items by clicking the "+" button and inputting the texts the required number of times.
            repeat with i from 2 to (count menuItems)
                click button 1 of group 1 of scroll area -1
                keystroke (item i of menuItems)
            end repeat
        end tell
    end tell
end tell


The user selects a name from the pop-up menu, which now exists, and completes the lesson details, charges, etc.

Then run Script 2 probably by pressing a key combination on the keyboard: the spreadsheet usually runs full screen on the laptop, so a key combination would simplify things (not essential, but convenient. I can assign the key combination).


Script 2

--Make an email, with a screen captured image of the spreadsheet page, ready to send to the address belonging to the selected name in the Pop-Up Menu 

set thePath to POSIX path of (path to documents folder) & "IMAGE" & ".jpg"
do shell script "screencapture -R20,80,720,880 -tjpg" & space & quoted form of the thePath

tell application "Mail"
    set myMessage to make new outgoing message
    set subject of myMessage to "Music Lessons 1st Term"
    set content of myMessage to " r r r "
    set theAttachedImage to "Macintosh HD:Users:petehunter:Documents:IMAGE.jpg"
    tell myMessage
        make new to recipient at end of to recipients with properties {name:" “} —Fake name for test purposes! The Script must provide this/these names
        make new attachment with properties {file name:theAttachedImage as alias}
    end tell
    activate -- brings Mail to front, for checking, and a manual send.
end tell


I hope this is clear enough. Thanks in advance for your help.

Macos: click on the Pro Tools button with AppleScript

friends. Happy New Year to all!

I am new with AppleScript. I need a script to click on a specific button in Avid Pro Tools.

I cannot correctly click on this button.

Here is the Hierarchy of the Accessibility Inspector

And here is the code I'm trying:

tell application "System Events"
    tell process "Pro Tools"
        tell (1st window whose title contains "Edit: ")

            click button "c - VOcal Comp (Vocal Comps)" of cell1 of row1 of table "Group List"

        end tell
    end tell
end tell

QuicKeys no longer runs on Catalina, how to run AppleScript on time?

Keyboard Maestro is a worthy replacement for QuicKeys, which can do this and much more with a GUI and a very active user community.

You could use launchd. Here is an example that will execute the file /Users/foo/my/applescript.scpt at 4:00 pm. (also known as 1600)


Keyboard Maestro can activate a Mac if desired. launchd It won't, but it will run when you wake up.

terminal: you cannot rename images sequentially through Applescript because other images already have that name

I am trying to automate the renaming of images sequentially using a simple Applescript. But I have difficulty telling the script that there are images in the same folder that already have the same name.

For example, I have three images of a cat called Hello world 1, Hello world 2 and Hello world 3.

In the same folder I also have three more of the same image but with the name Test Image 1, Test Image 2 and Test Image 3.

enter the description of the image here

I want to rename all "Test Images" to Hello world 4, Hello world 5 and Hello world 6 but I get the following error:

error "Finder received an error: the operation cannot be completed because
there is already an element with that name "number -48

How do I tell the script that there are other images in this folder that have the same name and that you need to start from "Hello World 4" and continue renaming the rest of the images?

Note that I will use this as a script embedded in an application called Hazel ( and does not accept drivers. Therefore, I will need a solution that does not use a driver.

Here is the script I am using that gives me the error when I try to rename "Test images":

tell application "Finder"
    set all_files to every item of (choose file with prompt "Choose the Files you'd like to rename:" with multiple selections allowed) as list
    set new_name to "Hello World"
    repeat with index from 1 to the count of all_files
        set this_file to item index of all_files
        set {itemName, itemExtension} to {name, name extension} of this_file
        set index_prefix to " "
        if itemExtension is "" then
            set file_extension to ""
            set file_extension to "." & itemExtension
        end if
        set the name of this_file to new_name & index_prefix & index & file_extension as string
    end repeat
end tell

Automator – AppleScript – (condition) if the X button exists, click, if not, click the Y button

I have a hard time writing a script to open a web page and click on different buttons depending on whether the button exists.

I want it to be if the X button exists, then click the X button.
If the Y button comes out, click on the Y button.

Both buttons will not exist at the same time.

I tried to put in:

  if exists, 
then click 
end if

But it does not work.
So far, I have used the watch-me-do recording function and obtained the following:

on run {input, parameters}
    -- Click the “X” button.
    delay 5.615004
    set timeoutSeconds to 2.0
    set uiScript to "click UI Element "X" of UI Element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window "XXXXXXXXXXX" of application process "Safari""
    my doWithTimeout(uiScript, timeoutSeconds)

    -- Click the “Y” button.
    delay 1.079871
    set timeoutSeconds to 2.0
    set uiScript to "click UI Element "Y" of UI Element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window "YYYYYYYYYYYYY" of application process "Safari""
    my doWithTimeout(uiScript, timeoutSeconds)
    return input
end run

on doWithTimeout(uiScript, timeoutSeconds)
    set endDate to (current date) + timeoutSeconds
            run script "tell application "System Events"
" & uiScript & "
end tell"
            exit repeat
        on error errorMessage
            if ((current date) > endDate) then
                error "Can not " & uiScript
            end if
        end try
    end repeat
end doWithTimeout 

Any comments and addresses would be greatly appreciated! Many thanks!

menu bar: switch to a specific input source in OS X in Catalina with AppleScript

Unfortunately things have changed in macOS Catalina and you will have to activate that menu in a different way.

Monitoring example AppleScript code, shown below, works for me in macOS CatalinaHowever, as I was testing in a virtual machine, I don't know if the time span in the operation of the code, between clicking on the destination menu and the following code, is inherent in a problem in the virtual machine or will be the same in a physical machine.

I have not German LaTeX2 then i used to German on the other hand, although for convenience I have replaced it in the code After the test

The usual warnings apply for the use of this in: System preferences > Security and Privacy > Privacy > Accessibility

Example AppleScript code:

tell application "System Events" to ¬
    tell application process "TextInputMenuAgent" to ¬
        tell menu bar item 1 of menu bar 2
            click menu item "German LaTeX2" of menu 1
        end tell

This does the same in a different way, and one of these ways may be faster.

tell application "System Events" to ¬
    tell application process "TextInputMenuAgent" to ¬
        click menu bar item 1 of menu bar 2

tell application "System Events"
    keystroke "g"
    key code 36
end tell

Use the example AppleScript code in a animal trainer:

changeKeyboardLayout("German LaTeX2")

on changeKeyboardLayout(layoutName)
    tell application "System Events" to ¬
        tell application process "TextInputMenuAgent" to ¬
            tell menu bar item 1 of menu bar 2
                click menu item layoutName of menu 1
            end tell
end changeKeyboardLayout

Or the second version of the code:

changeKeyboardLayout("German LaTeX2")

on changeKeyboardLayout(layoutName)
    tell application "System Events" to ¬
        tell application process "TextInputMenuAgent" to ¬
            click menu bar item 1 of menu bar 2
    tell application "System Events"
        keystroke (first text item of layoutName)
        key code 36
    end tell
end changeKeyboardLayout

Note the example AppleScript code it's just that and it doesn't contain any error handling as appropriate. It is the user's responsibility to add any error handling as appropriate, necessary or desired. Check out the try declaration and error declaration at AppleScript language guide. See also, Working with errors.

applescript: incorrect validation loop for iMessage

I am trying to make a number checker in iMessage. We all know that there is no such API, so I am trying to use the available tools. The principal dealt with past issues on this site on this subject.

I made a loop that inserts a number in a string, and by the color of the number (blue – registered, red – missing), using the standard Digital Colors Meter utility determines the color of the number. Then, depending on the color, add a variable with an initial txt file number to the new one or start the script from the beginning. At least it should be.

But something goes wrong in the loop. Check the color, insert numbers, everything is fine, but apparently the wrong condition, apparently the wrong data types. Insert different numbers in the scatter. Some will be inserted, others not, regardless of the initial condition. What could be the reason? Where this error?

    tell application "Digital Color Meter" to activate
    tell application "System Events"
        tell process "Digital Color Meter"
            click menu item 1 of menu 1 of menu bar item 4 of menu bar 1
        end tell
        do shell script "/usr/bin/python -c "import objc;bndl = objc.loadBundle('CoreGraphics', globals(), '/System/Library/Frameworks/ApplicationServices.framework');objc.loadBundleFunctions(bndl, globals(), (('CGWarpMouseCursorPosition', 'v{CGPoint=dd}')));CGWarpMouseCursorPosition((" & 1002 & "," & 222 & "));""
        set rgbColors to words of (the clipboard)
    end tell
    if rgbColors is equal to {"77", "39", "38"} then
        exit repeat
        do shell script "echo " & quoted form of n & " >> /Users/oneone/Desktop/base2.txt"
    end if

Only the color verification condition for Digital Color Meter is presented here.
Through the shell, the color is read at the mouse position and the variable is added to the new file.

applescript – Apple Script – How do I check if there is a window with a specific title?

I would like to create an Apple script that verifies if there is an application window with a specific name (no ID or index). I used the following code in an earlier script, and it works:

tell application "Notes" to close (every window whose name is "Notes")

However, I cannot understand how to verify if there is a window with this title. This should be incredibly simple. How do i do it?

applescript: loop lines of a txt file

How to create a loop so that a variable is assigned a string from a txt file (which is on the desktop).
It is necessary that a variable, in turn, be assigned the value of the string and the txt file. And so, until the lines in the txt file run out (there may be many).

 tell application "Messages"
      set theBuddy to buddy "variable" of service "iCloud"
      send "Hi there" to theBuddy
 end tell

The "variable" value in the code is what should be taken from the file and changed until the end of the cycle.

terminal – applescript: properly escape the entry in the script invocation

I have the following fragment in a applescript:

          tell current session of current tab of current window
              write text "watch -n1 'kubectl get pods | grep -i"  & input'
              split horizontally with default profile
              split vertically with default profile
          end tell

But this does not work since I receive the following error:

script error: Expected end of line but an unknown token was found. (-2741)

I want the final result, since I will invoke the script as

myscript appname


watch -n1 'kubectl get pods | grep -i appname'

Any suggestions?