如何通过AppleScript根据菜单名称访问特定的上下文菜单?

时间:2018-09-13 06:32:48

标签: applescript

是否可以使用AppleScript在macOS中的应用程序中单击特定的上下文菜单?

例如在Chrome中,我想单击“复制图片地址”,但是我不想每次想要获取图片地址时都手动进行整个过程。

那么可以通过AppleScript自动按名称访问上下文菜单吗?

谢谢。

2 个答案:

答案 0 :(得分:-1)

如果您分解问题,就会发现没有比当前使用的方法更有效的解决方案了。让我们计算一下使用脚本解决方案所涉及的操作:

  1. 任何访问上下文菜单的脚本都需要通过单击鼠标或键盘快捷键来了解与上下文菜单相关的图像;

  2. 您运行的任何脚本都需要通过从菜单栏(多次单击鼠标)访问它或按热键来触发。

这两件事已经等于或超过了通常通过手动访问上下文菜单进行的操作次数,即:右键单击 + 单击 = 2动作。

另一方面,

如果 ,您正在寻求一个自动化的过程,通过该过程可以检索指向每个图片的链接一个网页,使用JavaScript可以节省大量的效率:

    Array.from(document.getElementsByTagName('img'),x=>x.src)

要将其合并到AppleScript中,您需要启用一个选项以允许JavaScript自动化 Chrome 浏览器。为此(我使用的是 Chrome 版本69),请从菜单查看>开发人员>允许来自Apple Events的JavaScript 进行选择,并确保已将其打勾。

然后测试此AppleScript代码,该代码应返回 Google Chrome 当前显示的任何页面的图像来源列表:

    tell application "Google Chrome" to tell ¬
        the front window to tell ¬
        the active tab to execute javascript ¬
        "Array.from(document.getElementsByTagName('img'),x=>x.src)"

答案 1 :(得分:-1)

此代码需要“Cliclick” is short for “Command-Line Interface Click”. It is a a tiny shell/Terminal application that will emulate mouse clicks or series of mouse clicks (including doubleclicks and control-clicks) at arbitrary screen coordinates. Moreover, it lets you move the mouse, get the current mouse coordinates, press modifier keys etc.,该代码可免费下载且易于安装。

如果您在Automator中创建新服务并将以下代码粘贴到Automator工作流程中的“运行AppleScript”命令中,则可以在系统偏好设置中创建键盘快捷方式,以启动该服务。使用Google Chrome浏览器,并将鼠标光标悬停在任何图像上……只需按定义的快捷键,它将图像地址复制到剪贴板中


在我的系统上。 cliclick在以下目录中:/ usr / local / bin / cliclick。由于这个位置,在我的AppleScript代码和终端应用程序中,我需要使用完整路径来单击cliclick来调用命令。例如:do shell script "/usr/local/bin/cliclick rc:."在AppleScript中告诉cliclick右键单击。

property theApp : "Google Chrome"
property menuItemName : "Copy Image Address"
property menuItemName2 : "Copy Link Address"
property mouseLocation : missing value
property menuItems : missing value

run firstTry

script firstTry
    tell application theApp to activate
    repeat while application theApp is not running
        delay 0.5
    end repeat
    tell application "System Events" to tell its application process theApp ¬
        to tell its attribute "AXEnhancedUserInterface"
        set value to true
    end tell
    delay 0.1
    set mouseLocation to do shell script "/usr/local/bin/cliclick  p"
    do shell script "/usr/local/bin/cliclick  rc:."
    delay 0.1
    tell application "System Events" to tell its application process theApp to tell its window 1 ¬
        to tell its scroll area 1 to tell its menu 1
        delay 0.1
        set menuItems to its name of menu items
        set menuItems to text of menuItems
        try
            click menu item menuItemName
        on error errMsg number errNum
            try
                click menu item menuItemName2
            on error errMsg number errNum
                key code 53 -- press esc key
                run my secondTry
            end try
        end try
    end tell
end script

script secondTry
    tell application theApp to activate
    repeat while application theApp is not running
        delay 0.5
    end repeat
    tell application "System Events" to tell its application process theApp ¬
        to tell its attribute "AXEnhancedUserInterface"
        set value to true
    end tell
    do shell script "/usr/local/bin/cliclick  m:" & mouseLocation
    do shell script "/usr/local/bin/cliclick  rc:."
    delay 0.3
    activate
    tell application theApp
        activate
        set theChoice to (choose from list menuItems ¬
            with title ¬
            "Choose A Link" with prompt ¬
            "Choose A Link" OK button name ¬
            "Click Menu Item" cancel button name "Cancel") as string
    end tell
    delay 0.1
    do shell script "/usr/local/bin/cliclick  m:" & mouseLocation
    do shell script "/usr/local/bin/cliclick  rc:."
    tell application "System Events" to tell its application process theApp to tell its window 1 ¬
        to tell its scroll area 1 to tell its menu 1
        delay 0.1
        try
            click menu item theChoice
        on error errMsg number errNum
            try
                click menu item theChoice
            on error errMsg number errNum
                key code 53 -- press esc key
            end try
        end try
    end tell
end script

这是创建Automator服务后使用键盘快捷键的示例。在此示例中,我的鼠标直接悬停在图像上。它复制了图像地址,然后在新的浏览器窗口中粘贴图像地址并转到其URL。

enter image description here


第二个示例显示了如果我的鼠标不在图像上方调用键盘快捷键会发生什么情况

enter image description here

相关问题