使用applescript模拟拖放

时间:2017-02-08 18:47:12

标签: applescript

如何使用applescript模拟拖放?我正在创建一个脚本,在截图文件夹中找到最新的图像并将其拖放到松弛窗口,以便将其发送给我正在与之交谈的人。

1 个答案:

答案 0 :(得分:1)

  

@unska我不熟悉Slack。你可以将剪贴板中的文件粘贴到松弛的窗口中吗?我认为一个好的解决方案可能是创建一个AppleScript,它会将最新的屏幕截图文件复制到剪贴板,然后将剪贴板粘贴到松弛的窗口 - wch1zpink昨天
  @ wch1zpink复制粘贴实际上有效。我只需要在它之后发送回车。谢谢! - unska 16小时前

我编写了这个脚本,它将截取屏幕截图,然后将其复制到剪贴板,然后激活Slack并将屏幕截图粘贴到您的消息中并发送。

do shell script "screencapture -c" -- This takes a screencapture and copies it to the clipboard.  

    -- Below, I added the different usage examples for screencapture right from the man page in Terminal

    (*
    usage: screencapture [-icMPmwsWxSCUtoa] [files]
      -c         force screen capture to go to the clipboard
      -b         capture Touch Bar - non-interactive modes only
      -C         capture the cursor as well as the screen. only in non-  interactive modes
      -d         display errors to the user graphically
      -i         capture screen interactively, by selection or window
               control key - causes screen shot to go to clipboard
               space key   - toggle between mouse selection and
                             window selection modes
               escape key  - cancels interactive screen shot
      -m         only capture the main monitor, undefined if -i is set
      -M         screen capture output will go to a new Mail message
      -o         in window capture mode, do not capture the shadow of the window
      -P         screen capture output will open in Preview
      -I         screen capture output will in a new Messages message
      -s         only allow mouse selection mode
      -S         in window capture mode, capture the screen not the window
      -t<format> image format to create, default is png (other options include pdf, jpg, tiff and other formats)
      -T<seconds> Take the picture after a delay of <seconds>, default is 5
      -w         only allow window selection mode
      -W         start interaction in window selection mode
      -x         do not play sounds
      -a         do not include windows attached to selected windows
      -r         do not add dpi meta data to image
      -l<windowid> capture this windowsid
      -R<x,y,w,h> capture screen rect
      -B<bundleid> screen capture output will open in app with bundleidBS
  files   where to save the screen capture, 1 file per screen
    *)

    delay 2
tell application "Slack"
    activate

    delay 5 -- this gives you time to put your cursor in the window where you want to paste your screencapture.  This was the only option I could think of because I do not have Slack so I do not know any of the UI elements or window names as to where to tell this script where to place the screencapture.

        tell application "System Events"
            key code 9 -- this will paste the screencapture from your clipboard into the active Slack window where your cursor is.
            delay 1
            key code 36 -- this should press the return key and send your message
        end tell
end tell

在脚本编辑器中,将此脚本另存为应用程序。然后,您可以使应用程序说话并使用听写命令启动它,并将其配置为使用键盘快捷键启动。您也可以按原样运行应用程序。