Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppleScript to create 'link' to specific Messages.app conversation?

Goal

I want to have a AppleScript that let's me get the ID or something similar of the selected Messages.app conversation/chat, and then later have a AppleScript which can open the correct Messages conversation/chat corresponding to this ID.

  • Get ID/reference to currently selected Messages.app chat
  • Open a particular Messages.app chat based on a ID/reference

What I have tried so far

With Mail.app I can do the following:

tell application "Mail" set selectedMessages to selection set theMessage to item 1 of selectedMessages set messageid to message id of theMessage -- Make URL (must use URL-encoded values for "<" and ">") set urlText to "message://" & "%3c" & messageid & "%3e" return urlText end tell

But with Messages.app, there is to selection object.


Tried to get the content of the clipboard to see if there is any ID's or something of value which can be used, but it looks like the clipboard access is not as powerful as it is through cocoa programming (where you can get a lot of meta data and alternative clipboard content).


Double click on a conversation so that it opens with it's own window. Tried to get the ID of this window, and then open it later. Didn't work.

like image 600
eivindml Avatar asked Aug 28 '17 23:08

eivindml


1 Answers

To use this AppleScript script, as a script (.scpt) or an application (.app), you need to first select the target conversation in the list of conversations in Messages.app that you want it to always return to thereafter, and initially run this twice. This then sets property theSelectedRow : 0 to property theSelectedRow : i where the value of i will be the row number of the conversation in the list of conversations that was selected when initially run twice. The script also sets property thisName : "" to a value that is the name contained within the description of UI element 1 of row i. Between the value of these two properties, the script will always set focus back to the selected target conversation in subsequent runs, providing it still exists, even as its row number changes. If the selected target conversation no longer exists, the user is notified.

Read the comments throughout the script so as to have an understanding of what the code is doing.

AppleScript code:

--  # These two properties are used to hold information about the target 
--  # conversation in order to always set focus to it when the script runs
--  # after the initial runs. Initially, the script must be run twice to have 
--  # the selected target conversation be returned to on subsequent runs.
--  # 
--  # Note: This is also true anytime the script is modified or compiled, in
--  # Script Editor, as that resets the value of a property to its original value.

property thisRowNumber : 0
property thisName : ""


--  # See if the shift modifier key was pressed. This allows 
--  # setting the focus to a different target conversation.
--  # Note: Unlike when the script is modified or compiled from within
--  # Script Editor, when invoking this, the change is immediate when
--  # done after the initial runs to have the values of the properties set.

if my shiftKeyWasDown() then set thisRowNumber to 0


--  ### Main ###

tell application "Messages"
    if running then
        my selectTargetConversation()
        if minimized of window "Messages" is true then
            set minimized of window "Messages" to false
        end if
    else
        activate
        delay 2 -- # Allow time for Messages.app to open, adjust as/if necessary.
        my selectTargetConversation()
    end if
    activate
end tell


--  ### Handlers ###


--  # Detects if the shift modifier key was pressed.

on shiftKeyWasDown()
    if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSShiftKeyMask '") > 1 then
        return true
    else
        return false
    end if
end shiftKeyWasDown


--  # The 'getNameFromDescription' handler extracts the name portion of the 'description'
--  # within the 'UI element' of the 'row' of the target conversation. Example 'description':
--  # "Conversation with: Johnny Appleseed. Last activity: 6:10 PM. Last message: Look at all these trees!. "
--  # This is called from within a 'repeat' loop within the 'selectTargetConversation' handler:
--  # 'set thisName to my getNameFromDescription(description of UI element 1 of row i)'
--  # The 'thisName' property, in this example, would get set to: "Johnny Appleseed"

on getNameFromDescription(theText)
    set TID to AppleScript's text item delimiters
    set AppleScript's text item delimiters to {": "}
    set theText to text item 2 of theText
    set AppleScript's text item delimiters to {". "}
    set theText to text item 1 of theText
    set AppleScript's text item delimiters to TID
    return theText
end getNameFromDescription


--  # This handler provides all the logic necessary to ensure the 'Messages' window
--  # is available to set/reset focus back to the target conversation with each run,
--  # providing the target conversation still exists and sets focus to the target if it does.

on selectTargetConversation()

    tell application "System Events"

        --  # Make sure the 'Messages' window is available.
        --  # 
        --  # This branch of the 'if' block handles when Messages.app
        --  # is open, but without any windows showing.

        if (count of windows of application process "Messages") is equal to 0 then

            click UI element "Messages" of list 1 of application process "Dock"
            delay 0.25

        else
            --  # A least one window is open, make sure it's the 'Messages' window.

            --  # Set a flag to test against.

            set theMessagesWindowIsNotOpen to true

            set theWindowList to every window of application process "Messages"
            repeat with thisWindow in theWindowList
                if name of thisWindow is equal to "Messages" then
                    set theMessagesWindowIsNotOpen to false
                    exit repeat
                end if
            end repeat

            --  # If the value of 'theMessagesWindowIsNotOpen' is still 'true', 
            --  # then the 'Messages' window was not open, so open it.

            if theMessagesWindowIsNotOpen then
                tell application "Messages" to activate
                delay 0.25
                keystroke "0" using {command down}
            end if

        end if

        --  # When 'thisRowNumber is equal to 0' it's either the first time the script has run or it has been reset.
        --  # Get the 'row' number of the selected target conversation and set its value to 'thisRowNumber'.
        --  # Get the name within the 'description' of the 'UI element' of the selected 'row' and set it to 'thisName'.
        --  # Between the value of these two properties, the script will always set focus back to the selected target
        --  # conversation, providing it still exists. If the selected target conversation no longer exists, notify user.

        tell table 1 of scroll area 1 of splitter group 1 of window "Messages" of application process "Messages"

            if thisRowNumber is equal to 0 then

                repeat with i from 1 to (count rows)
                    if selected of row i is equal to true then
                        set thisRowNumber to i
                        set thisName to my getNameFromDescription(description of UI element 1 of row i)
                        exit repeat
                    end if
                end repeat

            else
                --  # Make sure the 'row' number, 'thisRowNumber', and the name within 'description of UI element' 
                --  # matches the value of the 'thisName' property, and if so, then set focus to it.

                if description of UI element 1 of row thisRowNumber contains thisName then

                    set selected of row thisRowNumber to true

                else
                    --  # The values no longer match. Ascertain the new 'row' number for 'thisRowNumber' that 
                    --  # contains the value of 'thisName', while verifying the target conversation still exists, and 
                    --  # reset focus back to the original selected target conversation, if it still exists.

                    --  # Set a flag to test against.

                    set theConversationNoLongerExists to true

                    repeat with i from 1 to (count rows)
                        if description of UI element 1 of row i contains thisName then
                            set thisRowNumber to i
                            set selected of row i to true
                            set theConversationNoLongerExists to false
                            exit repeat
                        end if
                    end repeat

                    --  # If the value of 'theConversationNoLongerExists' is still 'true',
                    --  #  then the conversation no longer exists, notify the user.

                    if theConversationNoLongerExists then
                        tell current application
                            display dialog "The target conversation has been deleted since the target was last set. Reset to a new target by selecting a conversation and then press the shift key down when running this script." buttons {"OK"} default button 1 with title "Target Conversation Reset Needed"
                        end tell
                    end if

                end if
            end if
        end tell
    end tell

end selectTargetConversation

Note: This was written and tested under OS X 10.8.5, however, I believe it will work as is under later versions of OS X/macOS and Messages.app, even with the current macOS 10.13 beta.

Additionally, the script employs minimal error handling and is absent of any try statements and on error handlers. Although aside from the value from the delay commands, that can be adjusted as/if needed, it should run fine without additional error handling. As always, the user can add/remove and or adjust the code as/if needed or wanted.

like image 154
user3439894 Avatar answered Oct 06 '22 18:10

user3439894