Agostino Agostino - 17 days ago 11
AppleScript Question

Saving screenshots with name of title bar

The title of screenshots taken with cmd+shit+4 is "Screen Shot DATE at TIME", which doesn't tell much. I'd like the title of my screenshots of applications to be like this "YYYYMMDD_HHMMSS Whatever Title Bar Text".

On Windows I use GreenShots, which by default does that, naming each screenshot of a windows with a timestamp followed by the name on the title bar.

On my OS X El Capitan, I can't find an Open Source app to do that, so I was suggested to use AppleScript instead.

What I have right now is close to a solution

do shell script "screencapture -W ~/tmp"
delay 3
tell application "System Events"
set frontApp to name of first application process whose frontmost is true
end tell
tell application frontApp
if the (count of windows) is not 0 then
set window_name to name of front window
end if
end tell
set screenShotName to do shell script "date +%Y%m%d_%H%M%S_"
set screenShotName to screenShotName & window_name
do shell script "mv ~/tmp ~/\"" & screenShotName & "\".png"


To save this, copy-paste it into Script Editor and save it as an app. To use it, double-click your screenshot app icon, then click on the window you want to take a screenshot of.

However, there are a few problems with this approach:


  • often I have to click twice on the window I want to take a screenshot of, the first to select it and the second to bring it into focus

  • it is far more laborious than what I'm used to do, I'd rather use a keyboard shortcut



Can this script be fixed to solve the above problems, or should I take a different approach?

Answer

I made several tests, and I think there is a bug in the way Apple handles Services. At the time Service is called, the list of windows of a selected process does not exist (!!). It exists if you call it from an application (that's the way I tested it !). During Service events, also UI elements are no longer available (which is still not the case when you run it from an application !!).

So, after several tests and try, I worked around this by making it an application and then asking Automator to launch that application.

There are few things to be done :

1) Enter this script is Script Editor

delay 2
set DefaultPath to POSIX path of (path to desktop folder)
set TimeStamp to do shell script "date +%Y%m%d_%H%M%S_"
tell application "System Events"
set frontApp to first application process whose frontmost is true
set FWindow to first window of frontApp
set FName to DefaultPath & TimeStamp & (name of FWindow) & ".jpg"
set {x, y} to position of FWindow
set {w, h} to size of FWindow
do shell script "screencapture -R " & x & "," & y & "," & w & "," & h & " " & quoted form of FName
end tell

2) Save that script as an Application in Script Editor (not as a script). Save it somewhere , anywhere is OK.

3) in your Automator Service, add action "Launch Application". Then, in this action, select "Application other..." and select the application saved in step 2.

4) open System preferences, "Security, tab "Confidentiality". Select "Accessibility" on left list, and click on "+" at bottom of the right list. Add and select your application saved in step 2. This will allow this application to drive your Mac.

It is working well for me !

The negative side of this workaround is that you have about 2 seconds, after you launch the service, to select to window you want to copy. If you do not, then the front most application will be the script itself...with no window !! I set 2 seconds for the delay to let you click on the window you want to capture, but you can reduce it to 1 second or 1.5... by changing the first line of the script.