How to take a portion screenshot by using QuickTime Player and save it to the clipboard in macOS when using Apple Script for automation?

245 views Asked by At

I'm a uni student taking online courses, I take portion screenshots by pressing Command+Control+Shift+4 every time to take notes, then I paste it to Notion. But it's not efficient to select the same area every time, then I found that QuickTime Player will keep the same area for capturing a portion screenshot, so I decided to use Apple Script to complete the operation.

The code I wrote could switch to the application QuickTime Player and then proceed to "New Screen Recording" in the menu by pressing Command+Control+N, but the problem is that sometimes pressing twice space will call out "Capture Selected Window" instead of "Capture Selected Portion". Meanwhile, the location where saving the screenshots will also be changed too though I already ticked "Remember Last Selection", maybe resetting the location to be saved every time is essential.

on run {input, parameters}
    
    activate application "QuickTime Player"
    
    tell application "System Events"
        
        key code 45 using {command down, control down}
        delay 0.1
        
        # Switch to "Capture Select Portion" by pressing SPACE
        repeat 2 times
            key code 49
            delay 0.1
        end repeat
        
        # Press ENTER to capture a screenshot
        key code 35
        delay 0.1
        
    end tell
    
    return input
end run

I spent a few hours finding the existed articles but perhaps not that fulfill my needs.

do shell script "screencapture -ci" is as same as pressing Command+Control+Shift+4, and I have to select the portion manually.

do shell script "screencapture -x -R20,20,640,380 ~/Desktop/test.png" needs a precise coordinate. But the coordinate is variable since the window is not fixed, and it needs to be found by using external software, maybe it's a little bit not efficient. If there is no solution for that, I will use it since it's much more convenient than selecting the portion every time.

Sorry about pasting others' code here and saying why they r not suitable for me, I just wanted to make it clear... Thanks for reading here, have a nice day no matter if u r willing to help me ^ ^

The part about pasting screenshots to Notion works, so I didn't paste it here.

Save to Clipboard instead of Preview

1

There are 1 answers

0
Rinsuki On

I tried writing something, it works but still has some bugs.

For example, the coordinate of the menu "Options" should be initialized depending on the precise location of the bar since I didn't succeed in finding the element by using entire contents.

You can't move your mouse during the use, or it might cause an error:

System Events got an error: Can’t get menu \"Options\" of button \"Options\" of window \"Window\" of application process \"screencaptureui\"." number -1728 from menu "Options" of button "Options" of window "Window" of application process "screencaptureui

When selecting the path where screenshots will be saved, it might take a few seconds to let the program find the menu item Clipboard.

tell application "System Events"
    
    tell process "QuickTime Player"
        
        # Switch to QuickTime Player (already opened expected, no launching time reserved)
        activate
        
        # Open "New Screen Recording" from the menu
        click menu item "New Screen Recording" of menu "File" of menu bar item "File" of menu bar 1 of application process "QuickTime Player" of application "System Events"
        
        delay 1
        
    end tell
    
    # Capture Selected Portion
    tell application process "screencaptureui"
        
        click checkbox "Capture Selected Portion" of window "Window" of application process "screencaptureui" of application "System Events"
        
        # Select Options from the menu (idk why if I replace it with click menu, the clipboard won't be found, needs to be fixed)
        click at {817, 824}
        delay 0.1
        
        # Save to the Clipboard
        click menu item "Clipboard" of menu "Options" of button "Options" of window "Window" of application process "screencaptureui" of application "System Events"
        delay 1
        
    end tell
    
    # Enter to capture the screenshot
    key code 36
    delay 0.1
    
    # Switch to Notion (already opened expected, no launching time reserved)
    tell application "Notion"
        activate
        
        delay 0.1
        
    end tell
    
    # Paste the screenshot to Notion
    key code 9 using {command down}
    
end tell

Hope this helps someone, if u hv any idea plz feel free to modify it since it still has bugs.