Saturday, December 16, 2017

#1 2015-03-18 05:05:15 pm

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Adjust ScreenCapture Settings.

Hello.

This is a script for adjusting screen capture settings, It is not the only way to do it, if you really want to start out with a 300DPI picture, which you then want to compress down, then there are tutorials out there that shows you how to do that by QuartzDebug. There are also some defaults setting, that people write can be used for faking a Retina Display.

The main purpose of this is to change any setting that you like. I admit that I use the "Show Current Folder" to open the last folder, (saves some time). I also change the part of the name that denotes the figure, to for example figure, and I also change the folder, so that I may have Screen Shots organized by project.

See here: for a script that uses the current settings when taking a snapshot of the frontmost window: MacScripter / any way to screencapture frontmost window? (post #11)

Applescript:

-- Copyright © 2015 McUsr, you are free to use, and share per email, but not to publish on a webpage.
# It is released under the BSD-3 license: (see [url]http://opensource.org/licenses/BSD-3-Clause)[/url]
# [url]http://macscripter.net/viewtopic.php?pid=179419#p179419[/url]
property scriptTitle : "Set Screen Capture Defaults"
property showCurSettings : "1. Display All Screen Capture Settings"
property setImgFolder : "2. Change Screen Shots Folder"
property setStemName : "3. Change Screen Shot Name"
property setPicType : "4. Change Picture Type"
property setShadowPre : "5. "
property setShadowPost : " Drop Shadow"
property setShadowOpt : "5. "
property openScreenCaptureFolder : "6. Open current Screen Shots Folder"
property saveAndQuit : "7. Save any Changes & Quit"
property quitWithoutSave : "8. Quit without Saving any changes"

property choicesList : {showCurSettings, setImgFolder, setStemName, setPicType, setShadowOpt, openScreenCaptureFolder, saveAndQuit, quitWithoutSave}

-- [url]http://osxdaily.com/2011/01/26/change-the-screenshot-save-file-location-in-mac-os-x/[/url]
-- [url]http://osxdaily.com/2012/02/16/change-screen-shot-file-name-mac-os-x/[/url]
-- [url]http://osxdaily.com/2010/08/16/change-the-screenshot-capture-file-format/[/url]

-- [url]http://www.tekrevue.com/tip/how-to-customize-screenshot-options-in-mac-os-x/[/url]
-- They mention the PICT format, but that just doesn't work.

-- [url]http://macscripter.net/viewtopic.php?id=38482[/url]

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:

[url]http://www.loc.gov/standards/iso639-2/php/English_list.php[/url]


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)
       
       # [url]http://macscripter.net/viewtopic.php?pid=179383#p179383[/url]
       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


on localizedFileNameString()
   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 locFileNameString to localized string of "Screen Shot" from table "ScreenCapture" in bundle correctResource
   return locFileNameString
end localizedFileNameString


on askToLeavePendingChanges()
   tell application (path to frontmost application as text)
       try
           display dialog "You have unsaved changes, do you really want to quit?" with title scriptTitle buttons {"No", "Yes"} default button 1 cancel button 1 with icon caution
           set ans to true
       on error
           set ans to false
       end try
   end tell
   return ans
end askToLeavePendingChanges

on run
   script ScreenCaptureParameters
       property imageTypes : {"BMP", "PNG", "JPG", "JP2", "TIFF", "TGA", "PDF", "GIF"}
       
       property oldLoc : missing value
       property newLoc : missing value
       property oldType : missing value
       property newType : missing value
       property oldStemName : missing value
       property newStemName : missing value
       property oldShadowOpt : missing value
       property newShadowOpt : missing value
       property home : missing value
       property changedSettings : false
       property curbid : missing value
       
       on writeSetting(setting, settingValue)
           if setting is not "disable-shadow" then
               set cmd to "defaults write com.apple.screencapture " & setting & space & settingValue
           else
               set cmd to "defaults write com.apple.screencapture disable-shadow -bool " & (settingValue as text)
           end if
           try
               do shell script cmd
           on error e number N
               tell application (path to frontmost application as text)
                   display alert scriptTitle & ":
                   Something went wrong during setting the new Screen Capture setting: "
& setting & "
"
& e & " #: " & N
                   return false
               end tell
           end try
           return true
       end writeSetting
       
       on saveSettings()
           if changedSettings then
               if newLoc is not missing value and newLoc ≠ oldLoc then
                   set restartSystemUi to writeSetting("location", quoted form of newLoc)
               end if
               
               if newStemName is not missing value and newStemName ≠ oldStemName then
                   set restartSystemUi to writeSetting("name", quoted form of newStemName)
               end if
               if newType is not missing value and newType ≠ oldType then
                   set restartSystemUi to writeSetting("type", newType)
               end if
               
               if newShadowOpt is not missing value and newShadowOpt ≠ oldShadowOpt then
                   set restartSystemUi to writeSetting("disable-shadow", newShadowOpt)
               end if
               
               if restartSystemUi then
                   tell application (path to frontmost application as text)
                       display notification "Restarting SystemUIServer within two seconds." with title scriptTitle subtitle "Screen Capture properties have changed"
                   end tell
                   
                   do shell script "{ sleep 2; killall SystemUIServer; } & 2>& 1 >/dev/null "
               end if
           else
               tell application (path to frontmost application as text)
                   display notification "There were no saved settings to change." with title scriptTitle subtitle "Quitting Script"
               end tell
           end if
       end saveSettings
       
       
       on displayCurSettings()
           getOldLoc()
           getOldType()
           getOldStemname()
           getOldShadowOpt()
           -- make the report:
           set rapport to "Those are the parameters currently in use:
           
Current save location:
"
& oldLoc
           if newLoc is not missing value then
               set rapport to rapport & "
New save location:
"
& newLoc
           end if
           set rapport to rapport & linefeed & linefeed & "Current Screen Shot name:
(The name will be augemented with date & time according to your country and language):
"
& oldStemName
           if newStemName is not missing value then
               set rapport to rapport & linefeed & "New Screen Shot name:
"
& newStemName
           end if
           set rapport to rapport & linefeed & linefeed & "Current picture type:
"
& oldType
           if newType is not missing value then
               set rapport to rapport & linefeed & "New picture type:
"
& newType
           end if
           set rapport to rapport & linefeed & linefeed & "Current Drop Shadow settings:
"

           if oldShadowOpt then
               set rapport to rapport & "Disabled"
           else
               set rapport to rapport & "Enabled"
           end if
           if newShadowOpt is not missing value then
               set rapport to rapport & linefeed & "New Drop Shadow settings:
"

               if newShadowOpt then
                   set rapport to rapport & "Disabled"
               else
                   set rapport to rapport & "Enabled"
               end if
           end if
           
           tell application (path to frontmost application as text)
               try
                   display dialog "

"
& rapport with title scriptTitle buttons {"Ok"} default button 1 cancel button 1 with icon 1
               end try
           end tell
       end displayCurSettings
       
       on tildeContracted from pxPath
           if home is missing value then set home to POSIX path of (path to home folder)
           set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, home}
           set pxPath to text items of pxPath
           set AppleScript's text item delimiters to "~/"
           set pxPath to pxPath as text
           set AppleScript's text item delimiters to tids
           return pxPath
       end tildeContracted
       
       on tildeExpand(pxPath)
           tell application id "sevs" to return POSIX path of item pxPath
       end tildeExpand
       
       
       on setWindowStateOnQuit()
           if curbid is not missing value and curbid is not "com.apple.finder" then do shell script "/usr/bin/open -b \"com.apple.finder\" "
       end setWindowStateOnQuit
       
       on displayScreenCaptureFolder()
           tell application id "sevs" to set curbid to bundle identifier of first application process whose frontmost is true and visible is true
           if newLoc is missing value then
               getOldLoc()
               set pth to (POSIX file tildeExpand(oldLoc)) as alias
           else
               set pth to (POSIX file tildeExpand(newLoc)) as alias
           end if
           tell application id "MACS"
               try
                   reveal first file of folder pth
               on error
                   open pth
               end try
           end tell
           if curbid is not "com.apple.finder" then
               do shell script "/usr/bin/open -b \"com.apple.finder\" >/dev/null 2>&1 &"
               delay 0.2
               tell application id "sevs" to set frontmost of (first application process whose bundle identifier is curbid) to true
           end if
       end displayScreenCaptureFolder
       
       on getOldLoc()
           if oldLoc is missing value then
               try
                   set oldLoc to do shell script "defaults read com.apple.screencapture location"
                   set oldLoc to tildeContracted from oldLoc
               on error
                   set oldLoc to "~/Desktop/"
               end try
           end if
       end getOldLoc
       
       on getOldType()
           try
               if oldType is missing value then set oldType to do shell script "defaults read com.apple.screencapture type"
           on error
               set oldType to "PNG"
           end try
       end getOldType
       
       on getOldStemname()
           try
               if oldStemName is missing value then set oldStemName to do shell script "defaults read com.apple.screencapture name"
           on error
               set oldStemName to localizedFileNameString()
           end try
       end getOldStemname
       
       on getOldShadowOpt()
           -- thanks to Mark Hunte for pointing out, that this key isn't set by default
           try
               if oldShadowOpt is missing value then set oldShadowOpt to (do shell script "defaults read com.apple.screencapture disable-shadow") as integer as boolean
           on error
               set oldShadowOpt to false
           end try
       end getOldShadowOpt
       
       on chooseScreenCaptureFolder()
           set failed to false
           getOldLoc()
           try
               tell application (path to frontmost application as text)
                   set theF to POSIX path of (choose folder with prompt "Choose folder to store Screen Capture images in")
               end tell
           on error
               set failed to true
           end try
           
           if not failed then
               set theF to tildeContracted from theF
               if theF ≠ oldLoc then
                   set my changedSettings to true
                   set newLoc to theF
                   tell application (path to frontmost application as text)
                       display notification "The folder for storing images have been changed." with title scriptTitle subtitle "Changing image folder"
                   end tell
               else
                   tell application (path to frontmost application as text)
                       display notification "Old folder and New folder are the same!" with title scriptTitle subtitle "Changing image folder"
                   end tell
                   
               end if
           end if
       end chooseScreenCaptureFolder
       
       on chooseNewStemName()
           
           if newStemName is not missing value then
               set defAns to newStemName
           else
               getOldStemname()
               set defAns to oldStemName
           end if
           
           set changed to false
           tell application (path to frontmost application as text)
               try
                   set theStem to text returned of (display dialog "Enter a new stem name for the Screen Capture images." with title scriptTitle default answer defAns with icon 1)
                   if theStem ≠ defAns then set changed to true
               end try
               if changed then
                   set newStemName to theStem
                   set changedSettings to true
                   display notification "The stemname for stored images have changed." with title scriptTitle subtitle "Changing stem names."
               end if
           end tell
       end chooseNewStemName
       
       on choosePictureType()
           getOldType()
           set newImageTypes to {}
           repeat with i from 1 to (count imageTypes)
               if item i of imageTypes is not oldType then set end of newImageTypes to item i of imageTypes
           end repeat
           tell application (path to frontmost application as text)
               set imgTypeChoice to choose from list newImageTypes default items item 1 of newImageTypes with title scriptTitle with prompt "Change image type from: " & oldType
               if imgTypeChoice is not false then
                   set newType to imgTypeChoice
                   set changedSettings to true
                   
                   display notification "The format for image capture files have changed." with title scriptTitle subtitle "Changing image format."
               end if
           end tell
       end choosePictureType
       
       on setShadowDropSetting()
           getOldShadowOpt()
           
           if newShadowOpt is missing value then
               set newShadowOpt to not oldShadowOpt
           else
               set newShadowOpt to not newShadowOpt
           end if
           set changedSettings to true
           tell application (path to frontmost application as text)
               display notification "The drop shadow setting has been changed." with title scriptTitle subtitle "Changing Drop Shadow Setting"
           end tell
           return newShadowOpt
       end setShadowDropSetting
       
   end script
   
   set i to 1
   set shallSave to false
   
   ScreenCaptureParameters's getOldShadowOpt()
   
   if ScreenCaptureParameters's oldShadowOpt then
       set setShadowOpt to setShadowPre & "Enable" & setShadowPost
   else
       set setShadowOpt to setShadowPre & "Disable" & setShadowPost
   end if
   set item 5 of choicesList to setShadowOpt
   with timeout of 600 seconds
       repeat
           tell application (path to frontmost application as text)
               set choice to choose from list choicesList default items item i of choicesList with title scriptTitle with prompt "Choose a Command"
           end tell
           if choice is false then
               
               if ScreenCaptureParameters's changedSettings then
                   set ans to askToLeavePendingChanges()
                   if ans then exit repeat
               else
                   exit repeat
               end if
           else
               set choice to item 1 of choice
               if choice is showCurSettings then
                   set i to 1
                   ScreenCaptureParameters's displayCurSettings()
               else if choice is setImgFolder then
                   set i to 2
                   ScreenCaptureParameters's chooseScreenCaptureFolder()
               else if choice is setStemName then
                   set i to 3
                   ScreenCaptureParameters's chooseNewStemName()
               else if choice is setPicType then
                   set i to 4
                   ScreenCaptureParameters's choosePictureType()
               else if choice is setShadowOpt then
                   set i to 5
                   if (ScreenCaptureParameters's setShadowDropSetting()) then
                       set setShadowOpt to setShadowPre & "Enable" & setShadowPost
                   else
                       set setShadowOpt to setShadowPre & "Disable" & setShadowPost
                   end if
                   set item 5 of choicesList to setShadowOpt
               else if choice is openScreenCaptureFolder then
                   set i to 6
                   ScreenCaptureParameters's displayScreenCaptureFolder()
               else if choice is saveAndQuit then
                   set shallSave to true
                   exit repeat
               else if choice is quitWithoutSave then
                   exit repeat
               end if
           end if
       end repeat
   end timeout
   
   if shallSave then ScreenCaptureParameters's saveSettings()
   ScreenCaptureParameters's setWindowStateOnQuit()
end run

Edit

I forgot to add that using PDF, gives you screenshots with 150 DPI right away.

Last edited by McUsrII (2015-03-19 03:55:55 pm)

Offline

 

#2 2015-03-18 07:09:56 pm

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Adjust ScreenCapture Settings.

Hi,

My run of this produced 

error "2015-03-19 00:05:46.701 defaults[38749:2326882]
The domain/default pair of (com.apple.screencapture, disable-shadow) does not exist" number 1


on

Applescript:

on getOldShadowOpt()
           if oldShadowOpt is missing value then set oldShadowOpt to (do shell script "defaults read com.apple.screencapture disable-shadow") as integer as boolean
       end getOldShadowOpt

I suspect there may be others that I do not have..


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/

Offline

 

#3 2015-03-18 07:48:09 pm

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Adjust ScreenCapture Settings.

mark hunte wrote:

Hi,

My run of this produced 

error "2015-03-19 00:05:46.701 defaults[38749:2326882]
The domain/default pair of (com.apple.screencapture, disable-shadow) does not exist" number 1


on

Applescript:

on getOldShadowOpt()
           if oldShadowOpt is missing value then set oldShadowOpt to (do shell script "defaults read com.apple.screencapture disable-shadow") as integer as boolean
       end getOldShadowOpt

I suspect there may be others that I do not have..


Hello Mark.

Thank you very much for pointing that out, it is now fixed, it is obviously one of those settings, that takes on a default setting until it is set for the first time, so the key doesn't exist up front. I played with the default settings in the Terminal window before I wrote the script, so the issue slipped under the radar for me.

I really think you should have the others, but I have set both the location, and type to an initial value, I don't dare to do that with the stem name of the picture, could you please try option 1, "Display current settings", and see if that works for you.

Thanks. smile

Edit
I had set the oldType to "JPG", but I investigated a little, and realized that it should be "PNG", that is now fixed.

While I wait for test results, regarding the setting of the name for images, let me just say that I firmly believe that the image name is written into the defaults database, at least for a user without an english locale, since the image name is localized. But, we'll see. wink

Last edited by McUsrII (2015-03-18 08:23:27 pm)

Offline

 

#4 2015-03-18 08:40:19 pm

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Adjust ScreenCapture Settings.

Hello.

I have tested it on a "clean account", and the screen capture filename is not set in the defaults database either.

I'll have to figure out how to get the original filename from the resources in the SystemUIServer bundle, in the mean time, I have set the name to "default ScreenCapture name" as to not create any run time errors.

I apologize for the inconvenience! hmm

Offline

 

#5 2015-03-18 09:16:18 pm

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Adjust ScreenCapture Settings.

Hello.

Every issue concerning unset ScreenCapture Defaults are now solved, when the filename is unset, the correct localized filename for Screen Capture pictures are returned, the other default values should also be returned.

Again: I am sorry for any inconvenience, but all issues should have been corrected now.

Thanks to Mark Hunte for alerting me. smile


Filed under: screencapture, settings

Offline

 

#6 2015-03-19 03:45:28 pm

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: Adjust ScreenCapture Settings.

I have to say that this is a neat script, thanks for sharing.


One thing I do notice is that option 1 says my file type is Jpg  but my files are actually png's (default)

I see you mentioned this but it looks like you  forgot to change that bit a code here

Last edited by mark hunte (2015-03-19 03:45:51 pm)


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/

Offline

 

#7 2015-03-19 03:57:14 pm

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Adjust ScreenCapture Settings.

mark hunte wrote:

I have to say that this is a neat script, thanks for sharing.


One thing I do notice is that option 1 says my file type is Jpg  but my files are actually png's (default)

I see you mentioned this but it looks like you  forgot to change that bit a code here


Thank you very much Mark, and I have fixed it, I was totally sure that I had set the default to PNG, but it is set to PNG now anyways.

I am grateful that you bothered to report the bugs. Thanks smile

Last edited by McUsrII (2015-03-19 03:58:13 pm)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)