0

This apple script seems to be doing exactly what I wanted it to do, (save screenshots both to a file location and to the clipboard) but I can't for the life of me figure out how to change this script to point at a different folder (currently it is saving to the Desktop)

I'm not very familiar with Automator or AppleScript and haven't been able to pin down where that is being set so that I can change it This line looks relevant:

set thePathFilename to POSIX path of (path to desktop folder as string) & theFilename

And this is the full script

on run {input, parameters}

--  # Screen Shot to Clipboard and File

--  # Clear the clipboard so the 'repeat until isReady ...' loop works properly.

set the clipboard to ""

--  # Copy picture of selected area to the clipboard, press: ⌃⇧⌘4
--  # Note that on my system I need to keystroke '$' instead of '4'.
--  # I assume this is because the 'shift' key is being pressed.        

tell application "System Events"
    keystroke "$" using {control down, shift down, command down}
end tell

--  # Wait while user makes the selection and releases the mouse or times out.
--  # Note that the time out also acts as an escape key press of sorts. In other
--  # words, if the user actually presses the escape key it has no effect on this
--  # script like it would if pressing the normal shortcut outside of the script.
--  #       
--  # As coded, the time out is 5 seconds. Adjust 'or i is greater than 10' and or  
--  # 'delay 0.5' as appropriate for your needs to set a different length time out.
--  # This means, as is, you have 5 seconds to select the area of the screen you
--  # want to capture and let go of the mouse button, otherwise it times out.

set i to 0
set isReady to false
repeat until isReady or i is greater than 10
    delay 0.5
    set i to i + 1
    set cbInfo to (clipboard info) as string
    if cbInfo contains "class PNGf" then
        set isReady to true
    end if
end repeat
if not isReady then
    --  # User either pressed the Esc key or timed out waiting.
    return --  # Exit the script without further processing.
end if

--  # Build out the screen shot path filename so its convention is of 
--  # the default behavior when saving a screen shot to the Desktop.

set theDateTimeNow to (do shell script "date \"+%Y-%m-%d at %l.%M.%S %p\"")
set theFilename to "Screen Shot " & theDateTimeNow & ".png"
set thePathFilename to POSIX path of (path to desktop folder as string) & theFilename

--  # Retrieve the PNG data from the clipboard and write it to a disk file.

set pngData to the clipboard as «class PNGf»
delay 0.5
try
    set fileNumber to open for access thePathFilename with write permission
    write pngData to fileNumber
    close access fileNumber
on error eStr number eNum
    try
        close access fileNumber
    end try
    activate
    display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with title "File I/O Error..." with icon caution
end try

--  # Convert the POSIX path filename to an alias.

set thePathFilename to POSIX file thePathFilename as alias

--  # Hide the file extension as is the default.

tell application "Finder"
    try
        set extension hidden of thePathFilename to true
    end try
end tell

Which I found here: Copy screenshot to clipboard in addition to saving the file

1
  • That line is indeed relevant - path to desktop is a Standard Additions command that returns an alias to the desktop. It is coerced to a string in order to concatenate the file name.
    – red_menace
    Nov 17, 2022 at 17:46

1 Answer 1

0

Thank you for confirming that! I know this is some real low hanging fruit for anyone that knows AppleScript but of all the languages I can program in, this syntax really throws me for a loop! I was able to get this working by changing that line to the following:

set thePathFilename to POSIX path of "/Users/username/examplefilepath/" & theFilename 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .