AppleScript アップルスクリプト 質問、発表 6
■ このスレッドは過去ログ倉庫に格納されています
最初はとっつきにくいだろうけど、
ASで文字列処理するならrubyなりperlなり使った方が圧倒的にラクだよ。
set rubySc to "
#encoding: utf-8
$KCODE='u'
str=ARGV[0]
puts str.gsub(/[\\r\\n]/, \"\")
"
set c to the clipboard
set c to quoted form of c
set rubySc to quoted form of rubySc
set myresult to do shell script "ruby -Ku -e " & rubySc & " " & c
エラーが出る場合(ruby1.9以降)は$KCODE='u'の頭に#つけて。(コメントアウト) >>748
クリップボードに加工したいデータがあるのなら、pbpaste コマンドで ruby スクリプトの標準入力にデータを渡すべき。引数にはデータ容量の制限があるので、大きなデータの場合はエラーになる。
程度問題だけど、プロセス起動のオーバーヘッドは馬鹿にならないので、ほとんどの場合(とくに処理するデータが少ない場合)は外部コマンドを起動するよりAppleScript で閉じたほうが速い。 >>749
pbpasteなんて知らんかった。ありがとうございます。
>>750
js使える環境ならそれでもいーですね。
一応pbpaste使った場合。>>
set rubySc to "
#encoding: utf-8
$KCODE='u'
str=`pbpaste`
puts str.gsub(/[\\r\\n]/, \"\")
"
set rubySc to quoted form of rubySc
set myresult to do shell script "ruby -Ku -e " & rubySc >>747
あざっす!!!
改行はreturnなんですね。ひょっとして…とおもったら
タブはtabでした!おかげさまで色々はかどりそうです! (alfredとかでホットキーを登録して、)
日時をテキストで挿入できるようにしたいのですが、
set the clipboard to (do shell script "date '+%Y-%m-%d %H:%M:%S'")
ここからどうすればいいでしょう?
これをやって、
さらにコマンドV、と2段階押さないといけない。
それを一回で済ませたい
なにかいい方法があれば教えてください。
(テキストを扱うどのアプリでもできるようにしたいです) AlfredでペーストするとこまでWorkflow組めるんじゃないの
続きは↓で
【コマンドラインランチャー】Alfredに惚れ込むスレ
http://potato.2ch.net/test/read.cgi/mac/1423312726/
AppleScriptでやるなら
tell application "System Events"
keystroke "v" using command down
end
とかでいけるけど >>755
それでできるんですが
ホットキー自体がコマンドとかシフトとかと組みあわせないといけないので、
おしたら、さっと指を離すようにしないと「ペースト(コマンド+v)」にならないですね・・・
とりあえずありがとうございます
スレ違いすみませんでした Macの起動項目で、アプリケーション形式にしたscriptを立ち上げているのですが
他の起動項目の一般アプリケーションより、そのスクリプトを前面に出したくて悩んだ結果、ソースの先頭に
tell application "そのスクリプト"
activate
end tell
で、前面にさせてるのですが
これだとスクリプト編集プログラムでデバッグしてるときと、アプリケーション形式に書き出したときとで
"スクリプト編集プログラム"を"そのスクリプト"に書き直さなければいけません
自分自身のことを指すmeみたいな名前で一本化できれば良いと思って探してるのですが
何か良案がありましたら、教えてください tell me
if not frontmost then
-- ここに757の3行入れる
end if
end tell
とか? tell me to activate じゃあかんのか アプリケーション形式ならactivateだけでも手前になるよ。
あと、スクリプト編集プログラムのツールバーに「アプリケーションを実行」ボタンが増えてる。
ただMavericksは無かったかも。 >>759-761
レスありがとうございます。meで行けるんですね ネットワークのフォルダのファイルを指定して開くようにしているけど、
別々の環境(自宅と職場)で50個のファイルがあるフォルダと、1000個の
ファイルがあるフォルダで表示にかかる時間がまったく違う。
これはファイルを探す時間がかかるって事? 状況が曖昧。条件がごっちゃになってる。
単に50個と1000個の時間差の話なら、自宅と職場はどういう関係があるの?
職場では50個も1000個も差がなくて、自宅では50個と1000個に差があるって話?
どういう事にせよ、フルパス指定なら時間差なんてないような気がするが。 すまん どちらも環境的にはレガシーなんだけど。
職場では導入間もないので50個、画像サーバ C2D Win7 画像フォルダはシステムと同じボリューム
クライアント MDD OSX10.4
自宅では1000個 画像サーバ CD VIsta 画像フォルダはシステムと別パーテーション
クライアント C2D OSX10.6
職場では瞬時に表示されるが、自宅では6秒以上かかる。
時間ができたら職場に1000個のUSBでも挿してテストしてみる。 それだけ色々違う状況じゃ全く比較にならんと思うが。
ボードやHDDの速度も違えばネットワーク速度、OSの処理速度どれもちがうだろ。
とりあえず自宅で50個のフォルダを試した方が早いんじゃないか?
それで遅けりゃファイル数の問題じゃないって事だろ。 1000個を捨てて、50個100個150個としてみた。
快速に表示された。
異常のあるファイルが混在していたのかもしれない。
検査するユーティリティでも探してみる。
スマソ。 超自己満スクリプト
do shell script "..." with administrator privileges でパスワード入れるのが面倒になってきた、
といってスクリプトにもろ書きして目に触れるのもなんか嫌だ
↓
・キーチェーンアクセスでパスワード項目を作成
アカウント/パスワード: applescript/パスワード
アクセス制御: securityコマンドに常に許可(コマンド実行時の初回のダイアログででも)
・スクリプト
set |password| to do shell script "security find-generic-password -a applescript -w"
do shell script "killall -HUP mDNSResponder" password |password| with administrator privileges >>768
これはおもしろい。毎回思考停止してパス入れてたからその発想がなかった
たいへん参考になりました スクリプトエディタのフォーマット設定が時々デフォルトに戻っちゃんだけど、この設定ってどこに保存してあるんでしょうね?
手作業で戻すのは面倒なのでファイル差し替えで戻せると良いんだけど 自己解決
$HOME/Library/Preferences/com.apple.applescript.plist
のようでした tell application "Image Events"
set theImage to open theDropItem as alias
tell theImage
set theDimensionsList to dimensions
end tell
close theImage
end tell
set imageHeight to item 2 of theDimensionsList
set imageWidth to item 1 of theDimensionsList
set padImageHeight to imageHeight + 50
set padImageWidth to imageWidth + 50
set scriptCommand to "sips " & (quoted form of theDropItemPOSIXPath) & " -s formatOptions 100% -p " & padImageHeight & " " & padImageWidth & " --padColor 555555 "
ここからさらに#999余白6pxを付加する記述を教えてください。
現在50px付加jpgをさらに6px付加スクリプトに投げてます。 間違ってるかもしれないが、2回処理(一旦保存)しないと無理なんじゃないかな?
連続して実行しても2番目の処理しか行われないっぽい。
あと、質問するときはそのままで動く物を載せた方がいい。 on open dropObjects
tell application "Image Events" to launch
repeat with theDropItem in dropObjects
set theDropItemPOSIXPath to POSIX path of theDropItem
tell application "Finder"
set theDropItemName to name of theDropItem
end tell
tell application "Image Events"
set theImage to open theDropItem as alias
tell theImage
set theDimensionsList to dimensions
end tell
close theImage
end tell 774の続き。
set imageHeight to item 2 of theDimensionsList
set imageWidth to item 1 of theDimensionsList
set padImageHeight to imageHeight + 50
set padImageWidth to imageWidth + 50
set scriptCommand to "sips " & (quoted form of theDropItemPOSIXPath)
& " -s formatOptions 100% -p " & padImageHeight & " " & padImageWidth
& " --padColor 555555 " & " --out " & "~/Desktop/" & (quoted form of ("pad" & theDropItemName))
do shell script scriptCommand
end repeat
end open
ここからさらに#999余白6pxを付加する記述を教えてください。
現在50px付加jpgをさらに6px付加スクリプトに投げてます。 もし二つのスクリプトが必要な場合、一つ目のスクリプトの最後に「保存後6px.app」に投げるを書き、
6px.appの処理の最後に「JPEGで上書き保存」を各方法を教えて頂けないでしょうか。
それも不可な場合、一つ目のスクリプトはこのままで、2つめのスクリプトの最後に「上書き保存」の記述を教えてください。。 丸投げのつもりなら >>1参照
多少なりとも理解しているなら、2回繰り返すだけ。 一回目のend repeatの直後に
tell application "Image Events" to launch..と続けてみましたが二回目の色指定余白しか付加できません。
なにが間違ってるんでしょうか。。 一回目のend repeatの直後に
tell application "Image Events" to launch..と続けてみましたが二回目の色指定余白しか付加できません。
なにが間違ってるんでしょうか。。 所望のオプション付けた sips を一つ追加、実行すればいいんじゃないの? >>779
元の画像に対して違う処理をしているだけだから。
一旦保存した画像に対してもう一度処理しないと。 1度目の処理でデスクトップに保存すると2度目の処理が最初のドロップアイテムに対して行ってしまったため
一度目は上書き保存、二度目でデスクトップに書き出しをしました。
出来れば一度目と二度目の後にデスクトップに書き出しできるドロップファイルをいじらずに済んで嬉しいんですが・・
set scriptCommand to "sips " & (quoted form of theDropItemPOSIXPath) & " -s formatOptions 100% -p " & padImageHeight & " " & padImageWidth & " --padColor FFFFFF "
do shell script scriptCommand
tell application "Image Events"←二度目処理・・ 色は判りづらいので変えてる。
ちなみにquoted formは要らんよ。
set padImageHeight to padImageHeight + 6
set padImageWidth to padImageWidth + 6
do shell script "sips " & "~/Desktop/pad" & theDropItemName & " -p " & padImageHeight & " " & padImageWidth & " --padColor FF0000" >>783
ありがとうございます!30余白が従来通りデスクトップ保存、二度目に教えたいただいた物を貼り付けましたが上手く動かず。。
「デスクトップに"padファイル名.jpg"がない」?とか言われます。。 >>784
そりゃ入れる場所間違ってるんじゃない?
やってること理解すればどこでエラーが起きてるのかわかる。 以下にて原画→枠線1追加→枠線2追加→上書き保存で妥協しました。
できれば原画→枠線1追加→枠線2追加→同名でデスクトップに新規保存、がしたかった。。
on open dropObjects
tell application "Image Events" to launch
repeat with theDropItem in dropObjects
set theDropItemPOSIXPath to POSIX path of theDropItem
tell application "Finder"
set theDropItemName to name of theDropItem
end tell
tell application "Image Events"
set theImage to open theDropItem as alias
tell theImage
set theDimensionsList to dimensions
end tell
close theImage
end tell
set imageHeight to item 2 of theDimensionsList
set imageWidth to item 1 of theDimensionsList
set padImageHeight to imageHeight + 50
set padImageWidth to imageWidth + 50 続き・・
set scriptCommand to "sips " & (quoted form of theDropItemPOSIXPath)
& " -s formatOptions 100% -p " & padImageHeight & " " & padImageWidth & " --padColor
55555 "
do shell script scriptCommand
tell application "Image Events"
set theImage to open theDropItem as alias
tell theImage
set theDimensionsList to dimensions
end tell
close theImage
end tell
set imageHeight to item 2 of theDimensionsList
set imageWidth to item 1 of theDimensionsList
set padImageHeight to imageHeight + 5
set padImageWidth to imageWidth + 5
set scriptCommand to "sips " & (quoted form of theDropItemPOSIXPath)
& " -s formatOptions 100% -p " & padImageHeight & " " & padImageWidth & " --padColor
888888 "
do shell script scriptCommand
end repeat
end open 自分でアップしたものを、ちょこちょこ変更するから動かなくなる。
俺の書いた部分と自分の書いた部分の差異もわからないなら変に触らないほうがいいよ。 全部 Image Events でやっちゃダメ?
on open drop_objects
repeat with drop_object in drop_objects
set drop_object to drop_object as string
tell application "Image Events"
repeat with item_modify_data in {{pad_margin:50, pad_color:{85 * 257, 85 * 257, 85 * 257}}, {pad_margin:6, pad_color:{153 * 257, 153 * 257, 153 * 257}}}
set this_image to open file drop_object
set the_dimensions to dimensions of this_image
pad this_image to dimensions {(item 1 of the_dimensions) + (pad_margin of item_modify_data), (item 2 of the_dimensions) + (pad_margin of item_modify_data)} with pad color pad_color of item_modify_data
save this_image
close this_image
end repeat
end tell
end repeat
end open
上書きしちゃうので元とっておきたいときは画像複製してからやってね >>788
ありがとうございます、教えて頂いた一行を入れて動かせるようにトライします。
>>789
確かに偶数じゃないと気持ちが悪いですね、直します。。
>>790
ありがとうございます、こんなに短い命令文で書けるんですか。。
ここの最後に「デスクトップに同名ファイル名・形式で保存」ができるようにトライしてみます。 ファイル名をコピー後に「A」を「B」に置換してからテキストを開いて貼り付け。
複数ファイルの場合は改行ではなくスペース区切りで貼り付けしてくれるスクリプト知ってたら教えてください! ls -1 | perl -lne 's/A/B/; push @l, $_; END { print join ↑ 後ろ切れちゃった
ls -1 | perl -lne 's/A/B/; push @l, $_; END { print join " ", @l }' | open -f >>795
ありがとうございます!!ただ言い忘れましたがapplescriptで教えてください!
ファイルをスクリプトアプリに入れるとそのファイル名から拡張子を除きAという文字をBに置換後に
テキスト(自分JEditです・・)が開いてそこにペーストされるという感じです! JEdit持ってないからTextEditで
複数ファイル渡すと別々に分かれることがある(理由不明)
on open someItems
set outNames to {}
repeat with anItem in someItems
tell application "Finder"
set aName to name of anItem
set anExte to name extension of anItem
if anExte is not "" then
set aName to text 1 thru ((length of aName) - (length of anExte) - 1) of aName
set anExte to "." & anExte
end if
considering case
if aName contains "A" then
set aName to do shell script "echo " & quoted form of aName & " | sed 's/A/B/'"
end if
end considering
set end of outNames to aName & anExte
end tell
end repeat
set oldASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {" "}
set outNames to outNames as text
set AppleScript's text item delimiters to oldASTID
set outNames to outNames & linefeed
tell application "TextEdit"
launch
set aDoc to make new document
set text of aDoc to outNames
activate
end tell
end open >>798
スクリプトの神様有難うございます!
お陰様でJEditにスペース区切りでペーストされました、が!
拡張子までペーストされてしまいます! >>796
>拡張子を除きAという文字をBに置換後に
どっちなんだろな〜と思いつつ、拡張子にあるAは置換しないと解釈することにして
拡張子はわざと残した
あとは適当にやってちょ〜よ ここまでおんぶにだっこで「されてしまいます!」じゃねえよ… どう見ても丸投げなんだから自分でどうこうできるわけがない >>801
まずは拡張子を外してから痴漢、最後に拡張子を戻してテキストにペーストしてたんですね!
この部分を削除して希望の通りのフォーマットでコピペされました!
神様有難うございます!! 文字列をパースしてdateオブジェクトに変換したいのだけど、システム設定に非依存でやる方法を教えてください。
set theDateString to 文字列をパースしてdateオブジェクトに変換したいのだけど、システム設定に非依存でやる方法を教えてください
set theDateString to "2016/9/6"
set theDate to date theDateString
の時、システム設定が和暦になってると theDateは "平成2016"になってしまいます
かといって、set theDateString to "H28/9/6"とすると、システム設定が西暦の場合エラーになります
システム設定のフォーマットに依存せず、テキストから正しく日付オブジェクトに変換する方法が思いつきません。 よろしくお願いします。 和暦と西暦の変換処理を自分で書いちゃうのはダメなの?
まず、dateオブジェクト内は西暦で統一しておく
dateオブジェクトに入れる前の文字列に
昭和とか平成、Hの文字が入っていないかチェックする処理
→入っていなくて年の部分が4文字なら西暦と見なしてdateオブジェクト化
→入っていた時は和暦→西暦変換の処理をしたあとdateオブジェクト化
で、dateオブジェクトから日時を取り出して使う際に、
西暦のまま取り出すか、和暦に変換して取り出すか、
フラグでも立てて分岐する処理を書いておく
江戸時代とか、それより前の日付も対象になるなら、
その分チェック対象のリスト(配列)も用意しないとダメで
面倒そうだけども… ちなみに自分の場合、
set theDate to current date
set (year of theDate) to 1999
こんな感じで、current date でdateオブジェクトをまず作っちゃって、
そのあと必要に応じて年月日を変更する形でやってて、
西暦しか使えないと思ってました…
>>809みたいな書き方すると、"2016/9/6" を直接
dateオブジェクト化したりできたんですね… >>809
かんたんかんたん
set theCommand to "date +%Y/%m/%d"
set theDateString to do shell script theCommand
set theDate to date theDateString あ、上のは検証してないんで一応補足しておくけど、もし年/月/日の並びがだめぽな
環境があるなら>>811のような定番の方法を使ってそれぞれ別個に定義してやればよい
e.g.
set theDate to current date
set theYearString to do shell script "date +%Y"
set (year of theDate) to theYearString
... OS10.9以上なら、Rubyのdateオブジェクトで西暦和暦変換ができるみたいです
ただ、和暦はドット区切り、西暦はスラッシュ区切り固定のようなので、
入力する日付を自分でその形式に変換しておく必要あり
(AppleScript's text item delimiters とif文を組み合わせればできる)
というか、本題からズレてたらすいません…
--和暦→西暦変換(ドット区切り・H(平成)・S(昭和)・T(大正)・M(明治)対応)
set theDateStringJp to "S10.9.6"
set theRubyScript to "require 'date';puts Date.parse(\"" & theDateStringJp & "\").strftime(\"%Y/%m/%d\")"
set theDateString to do shell script "ruby -Ku -e " & quoted form of theRubyScript
--西暦→和暦変換(スラッシュ区切り・H(平成)・S(昭和)・T(大正)・M(明治)対応)
set theDateString to "1935/09/06"
set theRubyScript to "require 'date';puts Date.parse(\"" & theDateString & "\").jisx0301"
set theDateStringJp to do shell script "ruby -Ku -e " & quoted form of theRubyScript >>809
set theDateString to "2016/9/6"
set theDate to date theDateString
set yearString to characters 1 thru 4 of theDateString as text
set (year of theDate) to yearString
log theDate
やっつけ仕事っぽいけど、
システムの設定が和暦だろうが西暦だろうが、
theDateStringの最初の4文字を切り出して
dateオブジェクト「theDate」のyearに代入しちゃえば、
西暦2016年(和暦表記だと平成28年)に上書きできますた 平成やHの文字が出てくると決めつけてるとすぐ使えなくなる可能性が… >>809
結局最終的に何が得たいの? 日付を西暦にした文字列? 日付同士の計算がしたい?
どちらにせよ一旦和暦を西暦に変換しないとできないっぽいので、
正規表現の使えないASのみではちょいと面倒だと思う。
ちなみに10.6.8だと>>809のは和暦にすると動かないんだが。 入力時点で表記を統一させるか、例外な表記は統一する関数でも書けよ image eventsで適当な画像をpictで保存したいのですがerror 1708がでてうまくいきません.
どのようにしたらいいでしょうか??
set afile to choose file
set adesk to (path to desktop) as text
tell application "Image Events"
launch
set the_image to open afile
try
save the_image as PICT in (file (adesk & "a.pict"))
end try
close the_image
end tell pictは扱えなさそうな感じ
代わりにsipsコマンドでやると Error: Unsupported output format com.apple.pict 標準のコマンドがダメなら brew install imagemagick して
convert コマンドを使う手もある
ImageMagick は PICT 形式に対応してるよ
ちなみに今時 PICT って何に使うんだい 過去スレでiTunesのアートワークとして画像を差し込む方法が載っていたのでその通りにやってみました.
そこでは一度,画像データをpictに変換し,data of artwork に入れていました. お騒がせしましたiTunes10.2から仕様が変わっていたのですね,,,
tell application "iTunes" to set data of artwork 1 of theTrack to (read (file targetImageFile) as picture)
これでできそうですね. そういうのは最初からやりたいことを全部言ったほうがいい
質問がズレてると回答もズレちまってお互い時間の無駄だ automatorで、上のメニューバーにはあるけどショートカットコマンドが与えられてない項目のショートカットを作りたいのですが、
自動記録したものを走らせてもエラーが出て動きません。
どのようなApple script を書けばいいか教えてください。
(例えばChromeの「ズーム」をショートカットにしたい場合) on run {input, parameters}
-- ズーム
set timeoutSeconds to 2.0
set uiScript to "click menu item 3 of menu 1 of menu bar item 9 of menu bar 1 of application process \"Chrome\""
my doWithTimeout(uiScript, timeoutSeconds)
return input
end run
on doWithTimeout(uiScript, timeoutSeconds)
set endDate to (current date) + timeoutSeconds
repeat
try
run script "tell application \"System Events\"
" & uiScript & "
end tell"
exit repeat
on error errorMessage
if ((current date) > endDate) then
error "Can not " & uiScript
end if
end try
end repeat
end doWithTimeout
記録したものはこんな感じです。 >>828で済むならいいけど書く場合
tell application id "com.apple.systemevents"
tell application "Google Chrome" to activate --これは呼び出し方によっては不要
tell process "Google Chrome"'s first menu bar's menu bar item "ウインドウ"'s menu "ウインドウ"'s menu item "ズーム" to click
end tell AppleScript他自動化ツールはこの先どうなる
https://www.macintouch.com/forums/showthread.php?tid=1189
Apple dumps Sal Soghoian, who gave us Mac automation tools いつの間にかiTunesでdisplay dialogとdisplay alertのcancel buttonが効かなくなってる >>830
やべぇよやべぇよ……
ほんこれ >Another nail in the coffin of macOS being a tool for professionals. Rhapsody発表時にAppleScriptも廃止する予定とジョブズが言ったとたん
じゃぁもうMac使ってる意味ねーよ安いAT機買うからとプロ業界から総スカンをくらい
なんと思い直したというMac自動化の歴史も、彼なき後、遂に終焉を迎えるわけですな 相談です。
最近、iTunes用の以下のスクリプトで動画ファイルの種類を変更しようとするとパラメーターのエラーです、というエラーが出てしまいます。
なぜか、TV showへの変更はうまくいき、movieやmusic videoへの変更が失敗します。
tell application "iTunes"
--return (item 1 of selection)
set video kind of (item 1 of selection) to movie
end tell home video/movie/music video/TV show
どれに変えてもエラーにならなかった バージョンは? コンパイルしなおしたか? できるファイルもあるのか? 何かの tell ブロックに入れ子になってたりして用語衝突してないか? Mac OS X 10.9.5
AppleScript 2.3.2
iTunes 12.4.3.1
でした。
いくつかのファイルを試しましたが、やはりTV showへの変更しかできませんでした。
他はエラーが返ります。
「iTunesでエラーが起きました:パラメーターのエラーです」
入れ子ではなく、スクリプトエディタでそのまま実行してもエラーになります。 気になる話があった
http://dougscripts.com/410
Set Video Kind of Selected v5.2
WARNING: a possible bug in iTunes 12.4.1 and later prevents this script from working. Until Apple confirms and/or addresses this issue, I do not recommend using this script. 原因不明で回避方法もないということでしょうか。
残念。 os 10.11です
デフォルトプリンタとデフォルトの用紙の設定をアップルスクリプトから制御したいのですが、プリンター設定ユーティリティがなくなってしまい、以前使っていたスクリプトが動かなくなりました
System Eventsからorg.cups.PrintingPrefs.plistを直接いじればいけるかなと思ったのですが、エラーになって設定できません
何かいい方法はありませんでしょうか ■ このスレッドは過去ログ倉庫に格納されています