sipsコマンドでリサイズするアップルスクリプト


iTunesのアートワーク用に画像を600x600にリサイズしたい。しかし、リサイズ用の手頃なアプリケーションが無い。以前、こちらをお薦めしたが、もっと簡単にしたかったのでsipsのコマンドをアップルスクリプトから呼び出すようにした。sipsについてはこちらの記事(OS X ハッキング! (73))(http://journal.mycom.co.jp/column/osx/073/index.html)を。

以下がそのスクリプト。OSX 10.4.11で確認。使い方は、ドロップレットにして画像ファイルをドロップする、あるいはFinderで画像ファイルを選択してスクリプトを実行する。画像ファイルと同じフォルダにリサイズ後の画像ファイルが作られる。

(*iTunes用のアートワーク作成用スクリプト*)
(*
600x600へリサイズする*)
property DO_LIST : {"png", "jpg"}
property MAX_SIZE : 600

on open theList
    
toControl(theList)
end open

on run
    --
Finder選択項目を調べる
    
tell application "Finder"
        
set theSelected to selection
        
set theLocationList to {}
        
repeat with theItem in theSelected
            
if class of theItem is folder then
                
exit repeat
            
end if
            
set theUrl to URL of theItem
            
set thePath to POSIX path of POSIX file theUrl
            --
ファイル名のURLデコード
            
set theCmd to "echo " & quoted form of thePath & " | perl -pe '~s/%([a-fA-F0-9][a-fA-F0-9])/pack(\"C\", hex($1))/eg;'"
            
set decodedItem to do shell script theCmd
            
set end of theLocationList to (POSIX file decodedItem as alias)
        
end repeat
    
end tell
    --
なければ選択ダイアログを表示
    
if theLocationList is {} then
        
choose file with prompt "画像ファイルを選んでください" without invisibles
        
toControl({result})
    
else
        
toControl(theLocationList) of me
    
end if
end run

on adding folder items to theFolder after receiving theList
    
toControl(theList)
end adding folder items to

on toControl(theList)
    
    
repeat with curItem in theList
        
set theFileInfo to info for curItem
        
if (name extension of theFileInfo) is in DO_LIST then
        
else
            
display alert "処理できません" message name of theFileInfo as warning
            
exit repeat
        
end if
        
        
        
set cmd to "sips -Z " & MAX_SIZE & " " & quoted form of POSIX path of curItem & " --out " & quoted form of POSIX path of newFile(curItem) of me
        
do shell script cmd
    
end repeat
    
end toControl

--
ファイル名に(resized)を付けて返す
--
newFile:HFS形式のStringを返す,oldFile:alias
on newFile(oldFile)
    
set theFileInfo to info for oldFile
    
set pareDir to (alias ((oldFile as string) & "::"))
    
set stringName to name of theFileInfo as string
    
set nameSize to (count of character of stringName) - (count of character of name extension of theFileInfo) - 1 -- 1 is dot size
    
set addName to "(resized)" as string
    
    
set newName to (items 1 thru nameSize of stringName) & addName & "." & name extension of theFileInfo as string
    
    
set returnName to pareDir & newName as string
    
return returnName
end newFile


Finderの選択項目を取り出すのに苦労している。

tell application "Finder"
    
first item of selection
end tell

まず、上記のような記述は出来ない。"Finder でエラーが起きました:item 1 of selection を取得できません。"と出てエラーとなる。

この場合、以下のように記述するとうまく行く。

tell application "Finder"
    
set theSelected to selection
    
first item of theSelected
end tell

あと、ファイルのパスはURLプロパティで取り出せる。ただし、日本語部分はエンコードされているので、alias形式にするためにperlを使ってデコードしている。

| | コメント (0) | トラックバック (0)

Unicode Text分解でのトラブル

またまたアップルスクリプトでトラブったので書く。

set testFile to alias "iMac TFT:Users:tbboy:Desktop:ピクチャ 7.png"

set theFileInfo to info for testFile
log characters of name of theFileInfo
log characters of (name of theFileInfo as string)

log "count = " & (count of items of name of theFileInfo)
log "count = " & (count of items of (name of theFileInfo as string))

set testList to characters of name of theFileInfo
log testList
set testString to testList as string
log testString

上のコードは、デスクトップに出来たスクリーンショットのファイル名を分割するテストコード。

log部分の実行結果は以下の通り。

    (*ヒ, ゜, ク, チ, ャ, , 7, ., p, n, g*)
    (*, ク, チ, ャ, , 7, ., p, n, g*)
    (*count = 11*)
    (*count = 10*)
    (*ヒ, ゜, ク, チ, ャ, , 7, ., p, n, g*)
    (*ヒ*)

name of theFileInfoの値は、Unicode TextとStringでは文字分解した時の結果が異なる。この程度ならまだ仕様の範疇かもしれないが、分解した文字を元の文字列へ合体させると失敗して「ヒ」だけになるのはあり得ないと思う。
この問題を回避するには、文字列分解時は必ずStringに変換してから分解することになる。

MacOSX 10.4.11で確認。

| | コメント (0) | トラックバック (0)

歌詞ファイルをテキストトラックに変換するアップルスクリプト(失敗)

以前、歌詞ファイルをQuickTimeのテキストトラックに変換する記事を書いたが、久々にカラオケ・タイムタグへの対応も追加しようとやってみるが挫折。。


まず、フォーマットが合わない。以下のテキストトラック抜粋を見れば効率の悪い変換だと分かるだろう。1曲で1000行以上のテキストになる。字幕の概念でカラオケを実現するのは難しいようだ。

[00:00:06.56]

[00:00:07.05]
哀 
[00:00:07.50]
哀 ふ
[00:00:07.74]
哀 ふる
[00:00:08.02]
哀 ふるえ
[00:00:08.38]
哀 ふるえる
[00:00:08.76]
哀 ふるえる哀
[00:00:09.36]
哀 ふるえる哀 
[00:00:09.86]
哀 ふるえる哀 そ
[00:00:10.05]
哀 ふるえる哀 それ
[00:00:10.23]
哀 ふるえる哀 それは
[00:00:10.51]
哀 ふるえる哀 それは 
[00:00:10.73]
哀 ふるえる哀 それは 別
[00:00:10.94]
哀 ふるえる哀 それは 別れ
[00:00:11.50]
哀 ふるえる哀 それは 別れ歌
[00:00:12.45]
哀 ふるえる哀 それは 別れ歌

また、同じ時間のタイムタグが存在するとQT Playerは、それらのテキストを連結して同時に表示してしまう。こちらとしては上書きで先行したテキストを消してほしいのだが。

結局、カラオケ・タイムタグを通常の行頭タイムタグに変換した方が収まりが良いようだ。

BigFatHairyDealにテキストトラックのタグ解説があって参考になりました。
できれば、ハイライト表示をやりたかったんだが今回の失敗で難しい事が判明しました。

| | コメント (0) | トラックバック (1)

miでtab区切りの選択列を取り出すアップルスクリプト

iTunesでライブラリの曲を複数選択し、コピーするとtab区切りのテキストになる。
で、これをblogに貼付けたりするとなると不要な列を取り除く作業が発生したりする。
iTunesで必要な列だけを表示してコピーという手段もあるが、手間がかかるのでアップルスクリプトで簡略化。

処理内容は、ダイアログを表示して必要な列を選択(複数選択化)すると選択文字列を必要列を残した文字列で置き換えるアップルスクリプト。


動作環境:OSX 10.4.10,mi 2.1.6
使い方:iTunesでコピーして、miへペースト。テキスト選択状態からスクリプトを実行。ダイアログ(一行目の各列をリスト表示)が出るので必要な列を選択してOKボタンを押す。

スクリーンショット:
Mireplacerow_ss

スクリプト:

(*選択した文字列で、タブ区切りの行列から指定した列を抽出*)
on run
    
tell application "mi"
        --
選択文字列はあるか?
        
if content of selection object 1 of front document is "" then
            
beep
            
return
        
end if
        --
1行目の列の取得
        
set theFirstRow to rows(item 1 of paragraphs of selection object 1 of front document) of me
        
        --
選択リストの作成
        
set theSelectList to {}
        
set tmpCount to 0
        
repeat with theItem in theFirstRow
            
set tmpCount to tmpCount + 1
            
set end of theSelectList to (tmpCount & ":" & theItem) as string
        
end repeat
        
        --
列の選択ダイアログ
        
set theSelectedRows to choose from list theSelectList with title "Select Rows" with multiple selections allowed
        
if (theSelectedRows is false) then
            
beep
            
return
        
end if
        
        --
指定した列を残して置換文字列の作成
        
set theText to {}
        
repeat with theLine in paragraphs of selection object 1 of front document
            
set theNewLine to {}
            
set theRows to rows(theLine as string) of me --theLineをそのまま渡すと空文字にされる
            
repeat with theRowNumber from 1 to count of theFirstRow
                
if item theRowNumber of theSelectList is in theSelectedRows then
                    --
列を追加
                    
if (count of theRows) ≧ theRowNumber then
                        --
2列目以降ならtabを挿入
                        
if (count of theNewLine) is not 0 then
                            
set end of theNewLine to tab
                        
end if
                        
set end of theNewLine to item theRowNumber of theRows
                    
end if
                
end if
            
end repeat
            --
改行文字列が含まれていない場合、改行文字を追加
            --
(最終列には改行文字が含まれている。この列を選択すると改行も含まれた状態になるため)
            
if return is not in every character of last item of theNewLine then
                
set end of theNewLine to return
            
end if
            --
置換行を追加
            
set end of theText to theNewLine
        
end repeat
        
        --
置換文字列で選択文字列を更新
        
set content of selection object 1 of document 1 to (theText as string)
    
end tell
    
end run

(*
tab区切りのCSVをリストに分解して返す*)
on rows(theLine)
    
set tmp to AppleScript's text item delimiters
    
set AppleScript's text item delimiters to tab
    
set theList to every text item of theLine
    
set AppleScript's text item delimiters to tmp
    
return theList
end rows

| | コメント (0) | トラックバック (1)