6

目标

我想要一个 AppleScript 让我获取所选 Messages.app 对话/聊天的 ID 或类似内容,然后再有一个 AppleScript 可以打开与此 ID 对应的正确消息对话/聊天。

  • 获取当前选定 Messages.app 聊天的 ID/引用
  • 根据 ID/参考打开特定的 Messages.app 聊天

到目前为止我尝试过的

使用 Mail.app 我可以执行以下操作:

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

但是对于 Messages.app,有selection反对意见。


试图获取的内容,the clipboard看看是否有任何 ID 或可以使用的有价值的东西,但看起来剪贴板访问不像通过可可编程那样强大(在那里你可以获得很多元数据和替代剪贴板内容)。


双击一个对话,以便它打开它自己的窗口。尝试获取此窗口的ID,稍后再打开。没用。

4

1 回答 1

1

要使用此AppleScript 脚本,作为脚本(.scpt) 或应用程序(.app),您需要首先在Messages.app中的对话列表中选择您希望它此后始终返回的目标对话,并且最初运行两次。然后将其设置为初始运行两次时选择的对话列表中的对话编号脚本还设置为名称_property theSelectedRow : 0property theSelectedRow : iirow property thisName : ""包含在description of UI element 1 of row i. 在这两个属性的之间,脚本将始终在后续运行中将焦点设置回选定的目标对话,前提是它仍然存在,即使它的数量发生变化。如果选定的目标对话不再存在,则通知用户。row

阅读整个脚本中的注释,以了解代码在做什么。

苹果脚本代码

--  # 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

注意:这是在 OS X 10.8.5 下编写和测试的,但是,我相信它可以更高版本的 OS X/macOS 和 Messages.app 下正常工作,即使是当前的 macOS 10.13 beta。

此外,该脚本使用最少的错误处理,并且没有任何try 语句on error 处理程序。尽管除了commands中的之外,可以根据需要进行调整,但它应该可以正常运行而无需额外的错误处理。与往常一样,用户可以根据需要或需要添加/删除和/或调整代码。delay

于 2017-09-08T22:16:26.957 回答