AppleScript创建特定Messages.app对话的"链接"?

eiv*_*dml 6 macos applescript message macos-high-sierra

目标

我想要一个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或值,但看起来剪贴板访问不像通过cocoa编程那样强大(你可以获得大量元数据和替代剪贴板内容).


双击对话,以便用它自己的窗口打开.试图获取此窗口的ID,然后稍后打开它.没工作.

use*_*894 1

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

阅读整个脚本中的注释,以便了解代码的作用

AppleScript代码

--  # 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
Run Code Online (Sandbox Code Playgroud)

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

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