使用 Automator Service 和 sips 调整 JPEG 压缩级别 - 遇到某些文件名

adjusting JPEG compression levels with an Automator Service and sips - stumbles over certain file names

我一直在修补一个脚本,该脚本使用 sips 作为 Service 在 Finder 中方便地调整 JPEG 文件压缩。与简单的 Automator compression/rescaling 功能不同,它维护文件标记。

唉,它偶然发现了某些文件名,例如带有 space 并且不会重新缩放它们的可能是因为文件输出在 space 字符上阻塞。因此,它在“DSC03761.JPG”之类的文件上按预期进行,但在“DSC03761 2.JPG”上却没有。如果路径包含 spaces,它也会失败,例如文件位于名为“my Pictures”的文件夹中。

由于我是菜鸟,一直没有想好怎么调整脚本。您可能有更好的主意?

screenshot of script

bash 脚本如下:

for f in "$@"; do

# Save creation date time stamp of the target file in 't'.
  t="$(/usr/bin/GetFileInfo -d "$f")"

# Compress the target file. Level 0-100 or low/normal=50/high/best
  filename=$f
  #/usr/bin/sips --setProperty formatOptions 60 $f --out ${filename%.*}.jpg
  /usr/bin/sips --setProperty formatOptions normal $f --out ${filename%.*}.jpg

# Set the modified and creation date time stamps to 't'.
  /usr/bin/SetFile -m "$t" "$f"
  /usr/bin/SetFile -d "$t" "$f"

done

# Notify user that operation is finished with a sound.
/usr/bin/afplay "/System/Library/Sounds/Purr.aiff"

在这里,您正确但不必要地引用了扩展:

  t="$(/usr/bin/GetFileInfo -d "$f")"

但是在这里,您需要使用引号的地方,您不需要:

  /usr/bin/sips --setProperty formatOptions 60 $f --out ${filename%.*}.jpg

后者应该是

  /usr/bin/sips --setProperty formatOptions 60 "$f" --out "${filename%.*}.jpg"