Reputation: 61
When I use ZSH to open multiple images using Preview programmatically, I get each Preview image stacked on top of each other. There is no way for the user to tell how many windows are piled on top of each other.
Code example:
# Loop through array of screenshot names
for screenImage in $screenShotsSaved; do
open -a preview $screenImage
done
The result looks like this:
I would prefer to have each of the Preview windows offset "a little" so each Preview window can be seen. Something like this:
Any ideas on how to offset these previewed images programmatically from ZSH?
I have tried using "-geometry" option (where the +0-30 is just an example):
open -a preview $screenImage -geometry +0-30
But the geometry argument gets interpreted as an image -which in turn generates an error:
The file /Users/ronfred/+0-30 does not exist.
I have tried including this applescript snippet into my zsh script to move each preview window using this code (where {1, 30} is just an example):
open -a preview $screenImage
osascript \
-e 'tell application "Preview"' \
-e 'set position of front window to {1, 30}' \
-e 'end tell'
But this script resulted in this error:
25:67: execution error: Preview got an error: Can’t make position of window 1 into type reference. (-1700)
Upvotes: 2
Views: 102
Reputation: 61
I created a test case to demonstrate solution using AppleScript.
#!/bin/zsh
# Test case:
# Capture screen images with screencapture,
# then display each of them with an increasing offset.
# Script calls builtin functions - screencapture, preview, applescript.
#
declare -i offset_x=10
declare -i offset_y=10
max_images=4
firstPreview=true
# Include a safe base file name for auto-crearted images.
test_image="./base_image_4968f9bb-5007-47a6-92d5-bdcd1a81e4_"
# Create and display some test images using screencapture and preview.
for ((i=1; i<=$max_images; i++)); do
# Create test image if not already available.
if [[ ! -e $test_image$i.png ]]; then
screencapture -R 100,100,50,50 $test_image$i.png
fi
# Open each test image in upper left corner of display
open -a preview $test_image$i.png
# Avoid system event error (-1719) on first reference to "Preview"
# in applescript.
# Consider using applescript repeat feature with "System Events"
if $firstPreview; then
sleep 1
firstPreview=false
fi
# Cascade/nudge each previewed image down and to left using applescript.
osascript - "$offset_x" "$offset_y" <<EOF
on run argv
tell application "System Events"
tell process "Preview"
set position of front window to {item 1 of argv, item 2 of argv}
end tell
end tell
end run
EOF
offset_x+=40
offset_y+=40
done
Upvotes: 0