1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

以前公開した AppleScript が動かない

 iPhone 15 Pro Max で撮影した画像ファイルが、1枚あたり、数GBと結構大きなサイズなので、パワーポイントなどに使用するには大きすぎる。横を1024ピクセル程度にすると数百KBになるので、フォルダに持ってきて一括で変換しようと、以前作成したスクリプトを使ったらダメだった。
以前(2016年3月)に書いた記事「Macで複数の画像を一括でリサイズする AppleScript (サムネイル化などに) のスクリプトが、今の環境では動作しないようだ。エラーも出ないで一見ちゃんと動いたように見えているが、画像のサイズは変わっていない。

最新の環境で作ってみた

 以前のスクリプトが動作しない原因はわからないが、Image Events が使えないようなので、他の方法でやってみることにした。
調べると、shellスクリプトに、Mac専用の sips という便利なコマンドが有った。
このコマンドについては「Macのターミナルで簡単に画像処理できるsipsの使い方」が参考になります。

完成したソース

フォルダを指定し、変更したいサイズ(ピクセル)をしているると、フォルダ内の全ての画像ファイルを一括でリサイズする、AppleScript

RisizeImage.scpt
-- 指定したフォルダー内の画像データを指定した幅になるよう縮小します。
-- 幅の指定はピクセル値で指定します。
-- 【注意】フォルダーまでのパスの中に、空白等が入っていると動作しません。
-- wtitten by ynomura.com

set outputFolder to POSIX path of (choose folder with prompt "サイズを変換する画像が入っているフォルダーを指定")
display dialog "返還後の幅をピクセル値で指定" default answer 150
set pixSize to text returned of result

set aList to list folder outputFolder
set procCount to 0
repeat with img in aList
   if not ((text of img) = ".DS_Store") then
   	set aFile to outputFolder & img
   	do shell script "sips -Z " & pixSize & " " & POSIX path of aFile
   	set procCount to procCount + 1
   end if
end repeat
-- end tell
display dialog (procCount as text) & " 件を " & pixSize & " にリサイズしました"

【追記】 Image Events を使う手もあるようです。

scaleImageTo1024.scpt
property scaleSize : 1024

on run
   set f to choose file
   processTheFiles({f})
end run

on open theFiles
   processTheFiles(theFiles)
end open

on processTheFiles(theFiles)
   tell application "Image Events" to launch
   repeat with f in theFiles
   	set thisFile to f as text
   	tell application "Image Events"
   		set a to open f
   		scale a to size scaleSize
   		save a in thisFile
   	end tell
   	delay 0.2
   end repeat
   tell application "Image Events" to quit
end processTheFiles

同じ処理をショートカットアプリで作ると

 最近、ショートカットアプリの凄さが分かったので、どうなのかな?とトライしてみたら、こんな感じでできた。
慣れないので、試行錯誤しながら作ったが、出来上がってみると、こんな簡単にできるのかと思う。
画像サイズ変更.png
 複数のファイルを処理するのに、repeat を考えなくてもできるのが楽ですが、メモリーの制限があるんだろうな。

どちらでもお好きな方を

 ショートカットアプリ版は、保存するフォルダを指定する手間がありますが、どちらでも同じことができます。お好きな方をお使いください。
まだまだどちらの環境も勉強中ですので、ご意見・ご指摘を歓迎します。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?