Calling AppleScript routine using osascript

Given the following AppleScript code

on myHandler()
    display dialog ("I'm your Handler!")
end myHandler

How do I call myHandler from osascript (using Terminal), plain without parameters, but also with?

How can I specify a drive in Applescript without the drive name?

I had a main system drive on my iMac named “Macintosh HD” and wrote my scripts to reference files on that drive. For instance, to get to my library directory, I had “Macintosh HD:Users:me:Documents:Dev:AppleScript:” as the full path.

The problem is that I’m having drive issues and need to replace my drive before it goes bad. To keep track, I cloned my original system drive (“Macintosh HD”) to “System Temp,” which is connected by USB. I can afford to use this drive for a few days, but need to return it to a server as soon as I can. Early this week I’ll be getting a new drive that will be the long term replacement drive, named, “System.”

I don’t want to name each drive “Macintosh HD” because that makes it hard for me to keep track of them, but I’d like to keep the scripts working throughout the process.

How can I, in Applescript, specify a pathname to a drive without using the HD name? If it’s the main HD, what is the equivalent of the Linux form of “/” for the root file system, so I’d use “/home/me” to get to my home directory? Is there some way to do something like “::Users:me” instead? I just want to be able to tell Applescript something like, “This is on the main system drive,” or, “Use the boot drive.”

I tried a lot of possible alternatives and none worked.

applescript – How do I organize screenshot based on window in Mac?

I tried to write a custom service in Mac with Automator to organize screenshots based on the window that is screenshot-ed. For example, if I use dictionary app in Mac in a full screen mode, and do screen capture, I would want all the screenshots go to a folder, say /Users/me/Dictionary so that I can later refer to all of them.

applescript – Add exif creation time with correct timezone (TZD) to filename

Hi i have a problem renaming a lot of photo and images files. I use a shellscript in automator to rename them by EXIF date and time. But the time is given by UTC and im living in europe with +0100 in vinter and +0200 in the summer.

Example: a picture from 2020-06-30 taken at 22:22:30
is named “2020-06-30 20/22/30 IMG_0001.jpg
instead of. “2020-06-30 22/22/30 IMG_0001.jpg

im using the script:

#/bin/zsh
for f in "$@"
do
    filedate=$(mdls $f | grep kMDItemContentCreationDate -m 1 | awk '{print $3, $4}');
    filename=$f:t
    filepath=$f:h
    mv $filepath/{"$filename","$filedate $filename"}
done

Can i somehow just add +0200 or +0100 to the “filedate…$4” and how?(and then manually order when the number of houser in the name is over 24) or is there a smarter way to ad time zone designator(TZD) to the script? I have not used script before so im completely new to all this. Hopw someone can help me.

applescript – How to display Notification from a LaunchDaemon process?

I have a LaunchDaemon process that needs to run as root user. The process internally calls osascript to display some notifications. Notifications display fine when the process is run manually without LaunchDaemon but when run with launchctl I get the following error when it tries to display a notification.

2020-07-05 21:39:02.350 osascript[48399:326313] NSNotificationCenter connection invalid
2020-07-05 21:45:19.023 osascript[48688:330683] NSNotificationCenter connection invalid
2020-07-05 21:45:19.023 osascript[48688:330683] Connection to notification center invalid. ServerConnectionFailure: 1 invalidated: 0
2020-07-05 21:45:19.023 osascript[48688:330683] Connection to notification center invalid. ServerConnectionFailure: 1 invalidated: 0

From searching on the Internet I have an impression that since LaunchDaemon processes can run without a user logged in, notifications cannot be shown. Is this correct? How do I workaround this?

applescript – Mail.app – compare two mailboxes?

I try to transfer 50 000 mails from an IMAP account A to another IMAP account B using Mail.app but it constantly fails during the process (that takes a day or so). There is a message-id-based duplicate filter on B so I am not afraid of duplicates but it is a PITA to figure out which messages are missing (≈ 100).

Is there a way to compare two mailboxes in Mail.app? Script?

Google didn’t return any obvious results. Would be nice to be able to do it both using message-id but also a combination of timestamp, to/from, subject and/or size and similar.

automator – Applescript variables won’t change

I am new to automator, and was trying out some commands with applescript. I was trying to open terminals at folders with quick actions and run some aliased shell commands on them:

#get Path variable 

tell application "Finder"
    set dir to (quoted form of POSIX path of (target of window 1 as alias))
    tell application "Terminal"
        reopen
        activate
        do script "clear" in window 1
        do script "exec zsh" in window 1
        do script "cd " & dir in window 1
        do script "newdir " & dir in window 1 #a custom command
    end tell
end tell

But the thing is, dir seemed to persist after every time I run the command, even on different folders. I can never quite seem to change it when I activate it on different folders. What am I missing here?

automator – How to use Applescript to open a printer app and select items within?

I have absolutely no knowledge or experience in using Applescript or Automator. I’m hoping by using either to create an app (or script) that will print a test page off my printer automatically. I know how to open the printer app (Canon iX6800 series.app), but I’m stuck from there with the dialog box not knowing how to continue to do the following steps:

  1. Run “Canon iX6800 series” – I got this part…
  2. Select “Settings” or Command S
  3. Select “Utility”
  4. Select “Print Test Page”
  5. Click “OK”
  6. Quit the app.

Any suggestions or help will be greatly appreciated. I can send the actual app to whoever might need it. Thanks in advance.
George

Clear recent file list in AppleScript?

I created an AppleScript for cleaning the clipboard which had this command:

tell application "System Events" to set the clipboard to ""

How would I create an AppleScript that did this with this shell script in Catalina latest version?

I tried:

do shell script "defaults delete -g NSNavRecentPlaces"

and got this error message:

error "2020-06-27 22:20:11.304 defaults(1830:54087) 
Domain (kCFPreferencesAnyApplication) not found.
Defaults have not been changed." number 1

How would I get an AppleScript to work for this sort of thing like the clipboard one I made?

Any advice appreciated on this!

Portable UI Applescript for choosing any menu item through any hierarchy of sub-menus

At the bottom of this page Apple explains how to use UI scripting to automate selecting menu items. There are two hardcoded examples that I’m drawing on in an effort to write a generic library script for use in a number of projects that can choose any menu item from any menu through any hierarchy of submenus.

The TL;DR version of this question is: How do I do that? 😉

But for some more detail, if desired…


…I’ve made some progress, and come up with a few approaches for this but each of them has one fatal flaw or another…

Option 1.

If all the parameters are text or lists of text: eg:

on ChooseMenuItem(theApp, theMenuName, theSubMenusNamesList, theMenuItemName)

which might be called with eg:

ChooseMenuItem("TextEdit", "Format", {"Font", "Ligatures"}, "Use Default")

then I can use a bunch of text parsing and concatenation etc. — including a repeat loop through the theSubMenusNamesList parameter to carefully construct a script in a string something like "tell menu ... click ... tell menu item ... click ... tell menu item ... click ..." etc. and then:

run script theScript in AppleScript

Voila! And this works. Except when any of those parameters – particularly the menu name – doesn’t exist. eg. trying to connect or disconnect a bluetooth device through the bluetooth menu extra. So…

Option 2.

If the menu is nameless then let’s change theMenuName to theMenu and pass it in by reference. eg:

tell application "System Events" to tell process "SystemUIServer" to set theMenu to (menu bar item 1 of menu bar 1 whose description contains "bluetooth")
...
ChooseMenuItem("SystemUIServer", theMenu, {"Magic Keyboard"}, "Connect")

Note, here theMenu variable and parameter as opposed to theMenuName. After performing the set statement, theMenu variable is not text, it’s an AppleScript object, something like:

menu bar item 6 of menu bar 1 of application process "SystemUIServer"

Proceeding from here this fails one of three ways.

2a. If I try to construct a script that includes: tell theMenu ... it tells me, not surprisingly, that …the variable theMenu is not defined.

2b. If I pull theMenu out of the quotes: "tell " & theMenu & "..." or "tell "" & theMenu & ""..." it errors with: Can’t make «class mbri» 6 of «class mbar» 1 of «class pcap» “SystemUIServer” of application “System Events” into type Unicode text.

ie. it can’t coerce the aforementioned menu bar item 6 of menu bar 1… into text (unlike in Option 1 where I could call the menu by name).

2c. If I try to pull theMenu’s tell statement out of the constructed text script so theScript text variable is something like tell menu item ... click ... tell menu item ... click (without the initial tell menu ... click) and then:

tell theMenu
    click
    run script theScript in AppleScript
end tell

then it tells me the variable "click" is not defined. I believe it’s referring to the first “click” in theScript variable that run script is trying to … well … run. run script... can’t seem to pick up any context from where it’s called from, or if it can I haven’t figured out how.

Option 3

I’ve tried to think through how I might somehow include actual tell menu item theSubMenuName ... statements (ie. not text strings of the same that I then try to run) inside the above-mentioned repeat loop, but since it’s a hierarchy of tells I can’t quite figure out how to make that work. Maybe there’s a recursive solution here, but my brain’s hurting too much to figure that out.

Conclusion

So, I’m at a loss… I can’t figure this out, but I also can’t believe there isn’t a way to do this. My inclination is that there has to be some way to refer to menu bar item 6 of menu bar 1 of… in my theScript variable somewhere… but how? Or if really not that, then some other ultimate solution here?