Hello.
This is my take on scripting ScreenCapture, it should make a ScreenCapture, with the saved snapshot following the same naming standard of a snapshot by ScreenCapture, when you do it manually. It also puts the resulting picture of the same type as manual Screen Capture , in the same folder, as it would have been with manual ScreenCapture. (cmd-opt 4, then space, then left mouse click).
use AppleScript version "2.3"
use scripting additions
# Copyright © 2015 McUsr and released under the BSD-3 license: (see http://opensource.org/licenses/BSD-3-Clause)
# in addition, you can't publish the handlers on a webpage, where those handlers can be perceived to be
# the main content, nor use the handlers in a book.
-- http://macscripter.net/viewtopic.php?pid=179402#p179402
script loc
(*
Documentation:
This handler is for returning a localized string, on
some machine from some app, according to the current users
language and locale settings.
How to use this handler:
You have to open the resources folder of the app that you
inted to retrieve the localized string from, and other than
assure that you have got the correct "key" for your localized
string, you have to make a list with any spelled out language
names, and a related list with the language codes, this page may
help when contstructing the lists:
http://www.loc.gov/standards/iso639-2/php/English_list.php
Returns:
The correct bundle according to the users language and locale
settings, from which we can get the correct localized string.
*)
on getTheLProjBundle(pxPathToResourcesFolder, langList, lProjList)
# http://macscripter.net/viewtopic.php?pid=179383#p179383
set sysLang to word 1 of (do shell script "defaults read -g AppleLanguages")
# StefanK
if (count langList) ≠(count lProjList) then error "getTheLProjBundle: langList is not as long as lProjList"
set pass to 1
repeat
if pass = 1 then
-- check if the first language is amongst the "big" languages.
if sysLang is in langList then
repeat with i from 1 to (count langList)
if sysLang is item i of langList then return item i of lProjList
end repeat
else
set pass to 2
end if
else if pass = 2 then
-- check if the current AppleLocales are among the very detailed localization bundles.
try
set curLocale to do shell script "/usr/bin/defaults read -g AppleLocale"
on error
set pass to 3
end try
if pass = 2 then -- still
try
tell application id "MACS"
set locBundleName to name of first folder of folder (POSIX file pxPathToResourcesFolder as text) whose name begins with curLocale
end tell
return locBundleName
on error
set pass to 3
end try
end if
else if pass = 3 then
-- check and see if the language, has a related, *normal region specific bundle*
try
set region to (do shell script "/usr/bin/grep \"" & sysLang & "_*\" /usr/share/locale/locale.alias |/usr/bin/head -1 |/usr/bin/sed -En 's/([[:alpha:]]+[[:blank:]]+)([^_]+)_([^.]+)(.*)/\\3/p' |/usr/bin/tr 'A-Z' 'a-z'")
on error
set pass to 4
end try
if pass = 3 then -- still . . .
try
tell application id "MACS"
set locBundleName to name of first folder of folder (POSIX file pxPathToResourcesFolder as text) whose name begins with region
end tell
return locBundleName
on error
set pass to 4
end try
end if
else if pass = 4 then
-- fall back
-- Thanks to Shane Stanley
tell application id "MACS"
set rcsFol to folder (POSIX file pxPathToResourcesFolder as text)
if exists folder "English.lproj" of rcsFol then
return "English.lproj"
else if exists folder "en.lproj" of rcsFol then
return "en.lproj"
else
error "getTheLProjBundle: No english localization bundle in this App!!"
end if
end tell
end if
end repeat
end getTheLProjBundle
end script
script dt
on ISO8601LikeDateAndClock()
-- Thanks to Dan Shockley for putting me in the right direction.
-- ISoLike: because I use dashes to separate the date elements.
-- like Apple do.
-- Other than that, the format for the date is YYYYY-MM-DD
-- The clock is whatever, including seconds, but the entities
-- are separated with dots like: HH.MM.SS
-- We deal with an am/pm clock or a 24 hour clock.
-- the 24 hour clock gets a strict two-digit format.
-- Am/pm are more "human". :)
tell (current date)
set D to its short date string
set numyear to year of it as integer
set numMonth to text -2 thru -1 of ("0" & (month of it as integer))
set numDay to text -2 thru -1 of ("0" & (day of it as integer))
set isolikeDate to (numyear & "-" & numMonth & "-" & numDay) as text
-- It is probably an easier way to do this . . .
set timeFormatProbe to its time string
if "am" is in timeFormatProbe then
set timeFormatType to "am"
else if "pm" is in timeFormatProbe then
set timeFormatType to "pm"
else
set timeFormatType to "" -- 24h clock
end if
if timeFormatType is not "" then -- not 24 hour clock!
if pm is timeFormatType then
set theHours to (hours of it) - 12
else
set theHours to (hours of it)
end if
set theMinutes to (minutes of it)
set theSeconds to (seconds of it)
set clocktime to theHours & "." & theMinutes & "." & theSeconds & space & timeFormatType
else
set theHours to text -2 thru -1 of ("0" & (hours of it))
set theMinutes to text -2 thru -1 of ("0" & (minutes of it))
set theSeconds to text -2 thru -1 of ("0" & (seconds of it))
set clocktime to theHours & "." & theMinutes & "." & theSeconds
end if
end tell
return {isolikeDate, clocktime}
end ISO8601LikeDateAndClock
end script
on FileNameFormatString()
set hfsAppName to "Macintosh HD:System:Library:CoreServices:SystemUIServer.app:"
-- WARNING! the hfsname is hardcoded, run the line below, and change the contents of hfsAppName with it.
-- return (path to application "SystemUIServer" as text)
set langList to {"nl", "en", "fr", "de", "it", "jp", "es"}
set lProjList to {"Dutch.lproj", "English.lproj", "French.lproj", "German.lproj", "Italian.lproj", "Japanese.lproj", "Spanish.lproj"}
set rscPath to POSIX path of ((hfsAppName & "Contents:Resources") as alias)
set correctLProj to loc's getTheLProjBundle(rscPath, langList, lProjList)
set correctResource to path to resource correctLProj in bundle file hfsAppName
set locString to localized string of "%@ %@ at %@" from table "ScreenCapture" in bundle correctResource
return locString
end FileNameFormatString
on ScreenShotStemName()
try
set stemName to do shell script "defaults read com.apple.screencapture name"
on error
set stemName to "Screen Shot"
end try
end ScreenShotStemName
on screenCaptureType()
try
set picType to do shell script "defaults read com.apple.screencapture type"
on error
set picType to "PNG"
-- This is an inaccuracy, it should have been "JPEG", but it works as of 2015-03-17.
end try
return picType
end screenCaptureType
on getPictureLocation()
try
set pictFolder to do shell script "defaults read com.apple.screencapture location"
tell application id "sevs"
set pictFolder to POSIX path of item pictFolder
end tell
on error
set pictFolder to POSIX path of (path to desktop folder)
end try
end getPictureLocation
on stemFileNameForScreenShot(picType)
-- You'll have to localize this one, so you'll have one that
-- gives the filenames like the screen capture
set formatStr to FileNameFormatString()
set stemName to ScreenShotStemName()
-- set picPath to getPictureLocation()
set {picDate, picTime} to dt's ISO8601LikeDateAndClock()
-- Assembly
set ofs1 to offset of space in formatStr
if ofs1 = 0 then error "The filename format string is probably localized for right to left reading: -->" & formatStr & "<--"
set intermed to stemName & text ofs1 thru -1 of formatStr
set ofs2 to offset of "%@" in intermed
if ofs2 = 0 then error "The filename format string is probably localized for right to left reading: -->" & formatStr & "<--"
set intermed to text 1 thru (ofs2 - 1) of intermed & picDate & text (ofs2 + 2) thru -1 of intermed
set ofs3 to offset of "%@" in intermed
set newFn to text 1 thru (ofs3 - 1) of intermed & picTime & "." & picType
return newFn
end stemFileNameForScreenShot
set picType to screenCaptureType()
set picName to getPictureLocation() & "/" & stemFileNameForScreenShot(picType)
-- Details concerning the screen capture
try
set dropShadow to (do shell script "defaults read com.apple.screencapture disable-shadow") as integer as boolean
on error
set dropShadow to false
end try
if dropShadow then
set shadowOpt to " -o "
else
set shadowOpt to ""
end if
-- http://macscripter.net/viewtopic.php?id=38482
tell application id "sevs" -- get frontmost process
set frontmostProcess to name of first application process where it is frontmost -- this will be the script process
end tell
tell application frontmostProcess to set winID to id of window 1
do shell script "screencapture -x " & shadowOpt & "-T 0 -t " & picType & " -l " & winID & space & quoted form of picName
-- -c is used to store it in the clipboard. -x is used to mute the sound.
-- -T is delay, default is a delay of 0 seconds.
-- -l is used to refer to the prefered windowid.
Edit
Added a comment inside FileNameFormatString() that the path to SystemUIServer is hardcoded, and added an out-commented line that may be run to get the actual path to it.
Edit 2015-03-19
I have set the image type to be of PNG, and not JPG, when it isn’t set in the defaults database.
I have also set the default value for disable drop shadows to false, as I am sure that the shadows are on by default.