当我使用 ZSH 以编程方式使用预览打开多个图像时,我将每个预览图像堆叠在一起。用户无法知道有多少个窗口堆叠在一起。
代码示例:
# Loop through array of screenshot names
for screenImage in $screenShotsSaved; do
open -a preview $screenImage
done
结果如下所示:
我希望每个预览窗口都偏移“一点”,以便可以看到每个预览窗口。像这样的东西:
关于如何以编程方式从 ZSH 偏移这些预览图像有什么想法吗?
我尝试过使用“-geometry”选项(其中+0-30只是一个例子):
open -a preview $screenImage -geometry +0-30
但是几何参数被解释为图像 - 这反过来会产生错误:
The file /Users/ronfred/+0-30 does not exist.
我尝试将此 applescript 片段包含到我的 zsh 脚本中,以使用此代码移动每个预览窗口(其中 {1, 30} 只是一个示例):
open -a preview $screenImage
osascript \
-e 'tell application "Preview"' \
-e 'set position of front window to {1, 30}' \
-e 'end tell'
但是这个脚本导致了这个错误:
25:67: execution error: Preview got an error: Can’t make position of window 1 into type reference. (-1700)
我创建了一个测试用例来演示使用 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
# 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