Post

Replies

Boosts

Views

Activity

Reply to AppleScript. Split the string into parts by the separator character.
Text item delimiters are what you are looking for, for example: set myString to "MyData1|MyData2" set {myVar1, myVar2} to splitString(myString) to splitString(someString) try set tempTID to AppleScript's text item delimiters -- save current delimiters set AppleScript's text item delimiters to "|" set pieces to text items of someString -- split the string set AppleScript's text item delimiters to tempTID -- restore old delimiters set firstPart to item 1 of pieces set secondPart to item 2 of pieces on error errmess -- delimiter not found log errmess return {firstPart, ""} -- empty string for missing item end try return {firstPart, secondPart} end splitString
Aug ’23
Reply to MenuItem scripting
It is one thing to learn about AppleScript (pay attention to object specifiers and references), but the scripting terminology of any given application is like the wild west - just about the time you think you have everything figured out, some mysterious animal you haven't seen before comes over the hill and eats all your hair. In System Events, an attribute is a class that is a named data value associated with a UI element. An attribute will have properties such as a name and value, but the attributes themselves vary depending on the UI object. The various class properties are documented in the scripting dictionary, but you will need to go exploring to find out what attributes are available and what they are. A lot of these attributes can be also figured out from the Cocoa API documentation. There are some examples out there, although most seem to be based on a few StackExchange topics from over a decade ago. I blew the dust off my version, it uses System Events to get an application's menu item shortcuts and writes them to a file. Note that the script scans every menu item, so depending on how many there are it may take a while. Rich Text and symbols for the various keys and their modifiers are used for readability. property glyphSymbols : {{2, "tab", "⇥"}, {4, "enter", "⌤"}, {9, "space", "␣"}, {10, "delete right", "⌦"}, {11, "return", "↩"}, {23, "delete left", "⌫"}, {27, "escape", "⎋"}, {28, "clear", "⌧"}, {98, "page up", "⇞"}, {100, "left arrow", "←"}, {101, "right arrow", "→"}, {104, "up arrow", "↑"}, {106, "down arrow", "↓"}, {107, "page down", "⇟"}, {111, "F1", "F1"}, {112, "F2", "F2"}, {113, "F3", "F3"}, {114, "F4", "F4"}, {115, "F5", "F5"}, {116, "F6", "F6"}, {117, "F7", "F7"}, {118, "F8", "F8"}, {119, "F9", "F9"}, {120, "F10", "F10"}, {121, "F11", "F11"}, {122, "F12", "F12"}, {135, "F13", "F13"}, {136, "F14", "F14"}, {137, "F15", "F15"}, {140, "eject", "⏏"}, {143, "F16", "F16"}} -- https://www.hammerspoon.org/docs/hs.application.html#menuGlyphs property modifierSymbols : {{"command", "⌘"}, {"shift-command", "⇧⌘"}, {"option-command", "⌥⌘"}, {"option-shift-command", "⌥⇧⌘"}, {"control-command", "⌃⌘"}, {"control-shift-command", "⌃⇧⌘"}, {"control-option-command", "⌃⌥⌘"}, {"control-option-shift-command", "⌃⌥⇧⌘"}, {"none", ""}, {"shift", "⇧"}, {"option", "⌥"}, {"option-shift", "⌥⇧"}, {"control", "⌃"}, {"control-shift", "⌃⇧"}, {"control-option", "⌃⌥"}, {"control-option-shift", "⌃⌥⇧"}} -- modifier mask bits (⌘ is assumed): 1 = ⇧, 2 = ⌥, 4 = ⌃, 8 = no ⌘, 16 = fn global ItemsScanned, totalFound -- sums for number of menu items scanned, number of shortcuts found on run -- example to get a list of menu item key equivalents (shortcuts) for an application set output to linefeed set {ItemsScanned, totalFound} to {0, 0} set appName to name of application ((choose file of type "com.apple.application") as text) tell application appName to activate tell application "System Events" to tell process appName -- note that process names can be different repeat with aMenu in (menus of menu bar items 2 thru -1 of menu bar 1) -- skip Apple menu set someResults to my (getshortcuts for aMenu) if someResults is not in {"", missing value} then set output to output & name of aMenu & ":" & linefeed & someResults end if end repeat end tell set outputPath to ((path to desktop) as text) & id of application appName & " menu item shortcuts.rtf" do shell script "echo " & quoted form of output & " | /usr/bin/textutil -stdin -convert rtf -output " & quoted form of POSIX path of outputPath -- rich text (for formatting and better symbols) return "" & ItemsScanned & " menu items scanned for " & totalFound & " shortcuts" -- global sums end run # Recursive handler to get modifiers and any key equivalent (shortcut) for menu items of a menu. to getshortcuts for someMenu given indent:indent : 1 set {shortcuts, prefix} to {"", ""} repeat indent times set prefix to prefix & tab end repeat tell application "System Events" to repeat with menuItem in (menu items of someMenu) tell menuItem to try set itemName to name of it if itemName is not in {"", missing value} then -- skip separator items and views set ItemsScanned to ItemsScanned + 1 -- global sum set symbol to my (getCommandSymbols for it) if symbol is not missing value then -- found one set totalFound to totalFound + 1 -- global sum set spacer to tab if (count symbol) < 3 or symbol begins with "fn" then set spacer to spacer & tab -- alignment set shortcuts to shortcuts & prefix & symbol & space & spacer & itemName & linefeed else -- try submenu repeat with submenu in menus of it -- keep reference indexes to work with duplicate names set theItems to my (getshortcuts for submenu given indent:(indent + 1)) if theItems is not in {"", missing value} then set shortcuts to shortcuts & prefix & itemName & ":" & linefeed & theItems end repeat end if end if on error errmess log "*** Error getting shortcut for " & (get name of it) & ": " & errmess end try end repeat return shortcuts end getshortcuts # Get character symbols as needed for a menu item command. to getCommandSymbols for menuItem tell application "System Events" to tell menuItem to try set cmdModifiers to value of attribute "AXMenuItemCmdModifiers" of it if cmdModifiers is 8 then -- none return missing value else if cmdModifiers is 24 then -- function key set modifier to "fn" else set modifier to last item of item (cmdModifiers + 1) of modifierSymbols -- mask starts at 0 end if set cmdChar to value of attribute "AXMenuItemCmdChar" of it if cmdChar is not in {"", missing value} then return modifier & cmdChar set cmdGlyph to value of attribute "AXMenuItemCmdGlyph" of it if cmdGlyph is not in {"", missing value} then repeat with glyph in glyphSymbols if contents of first item of glyph is cmdGlyph then return modifier & last item of glyph end repeat on error errmess log "*** Error getting command symbols for " & quoted form of (get name of it) & ": " & errmess end try return missing value end getCommandSymbols
Jul ’23
Reply to MenuItem scripting
The general idea would be to get the menus of the menu bar, then repeat through the menu items of each of those menus. Since a menu item can also have a menu, a recursive handler can be used to descend the menu structure. Once you have a regular menu item (no submenu), you can then get the values of its attributes to build the shortcut. Attributes of interest would be AXMenuItemCmdModifiers which is a mask of the modifier keys used (shift, option, etc), AXMenuItemCmdChar which is the character, and AXMenuItemCmdGlyph which is a code for a glyph for keys such as tab or backspace that don’t have a character. From there it is just a matter of formatting the results into something readable.
Jul ’23
Reply to i've noticed many times that applescript doesn't take this syntax
Make a note to post actual code, never pictures of code - you aren’t going to get many replies when people have to type in your script or debug formatting errors from the text recognition framework. The error is from trying to use Reminders to get the start date of a Calendar event. You need to target the appropriate application to use its terminology, for example: -- Set the names of your calendar and reminders set calendarName to "appts" set remindersName to "appts" -- Get the current date and time set currentDate to current date -- Get the events from the calendar tell application "Calendar" set calendarEvents to every event of calendar calendarName whose start date is greater than currentDate repeat with theEvent in calendarEvents set eventTitle to summary of theEvent set eventStartDate to start date of theEvent set eventNotes to description of theEvent if eventNotes is missing value then set eventNotes to "(no notes)" -- Create a new reminder with the event details tell application "Reminders" to tell list remindersName make new reminder with properties {name:eventTitle, due date:eventStartDate, body:eventNotes} end tell end repeat end tell
May ’23
Reply to Applescripting multi strings into one
To make sure a string is defined, you can initially declare it as an empty string, then replace the value as needed. Also, instead of 10 different variables, just use a list - that is what they are for. Since the strings are in a fixed order, items in a list can be assigned according to the selections, then the list coerced to a string, which will add them all together (selected or not): set stringsList to {"", "", "", "", "", "", "", "", "", ""} -- 10 items set the choices to {"Music Sharing", "Remove Network Icons", "Dual - Galley & Active", "HD video", "Hide Menu", "Hide Reactions", "Remove All Overlays", "Dual - Galley x2", "Remove Names", "Other"} set the response to (choose from list choices with prompt "Select Advanced Dial String Command(s). Use Command (⌘) to select multiple." with multiple selections allowed and empty selection allowed) if the response is not false then repeat with anItem in the response if contents of anItem is "Music Sharing" then set item 1 of stringsList to "306" if contents of anItem is "Remove Network Icons" then set item 2 of stringsList to "307" if contents of anItem is "Dual - Galley & Active" then set item 3 of stringsList to "308" if contents of anItem is "HD video" then set item 4 of stringsList to "309" if contents of anItem is "Hide Menu" then set item 5 of stringsList to "504" if contents of anItem is "Hide Reactions" then set item 6 of stringsList to "508" if contents of anItem is "Remove All Overlays" then set item 7 of stringsList to "606" if contents of anItem is "Dual - Galley x2" then set item 8 of stringsList to "608" if contents of anItem is "Remove Names" then set item 9 of stringsList to "102" if contents of anItem is "Other" then set item 10 of stringsList to text returned of (display dialog "Enter other advanced string command(s)" default answer "") end repeat return the stringsList as string
Jul ’22
Reply to Seeking suggestions - how to have versions of an Automator service workflow
If all you are doing is reading the XML file, I'm not sure what would corrupt it, but an idea similar to the AppleScript comment would be to add a variable to the workflow (both would be in the file). In a workflow's bundle, the /Contents/document.wflow file is a property list that declares the various actions with their settings (such as the script for the Run AppleScript action), action connections, workflow metadata, and any variables. By adding a variable for a version number to the workflow, your script can just look for the variable name and get its value instead of adding a file to the bundle - System Events includes a Property List Suite, so you can do something like: on run -- example set plistFile to (choose file with showing package contents) -- get the document.wflow file set variables to getPlistElement from {plistFile, "variables"} if variables is not missing value then repeat with anItem in variables -- multiple variables if |name| of anItem is "Version" then -- or whatever return first item of value of anItem -- the value is a list end if end repeat return missing value -- not found end run to getPlistElement from plistItems -- get specified element from plist structure by name or index try if (count plistItems) < 2 then error "item list contains too few items" tell application "System Events" set element to property list file ((first item of plistItems) as text) -- root element repeat with anItem in rest of plistItems -- add on the sub items set anItem to contents of anItem try -- handle an index number set anItem to anItem as integer end try set element to (get property list item anItem of element) end repeat return value of element end tell on error errmess number errnum -- not found, etc log "getPlistElement error: " & errmess & " (" & errnum & ")" -- or pass it on return missing value end try end getPlistElement
Feb ’22