Archive for the 'Automator' Category

02/06 指定のAutomator Workflowのファイルを実行する v3

指定のAutomator Workflow(Automator書類)を実行するAppleScriptです。

Mac OS X 10.5の途中のアップデートからAutomator RunnerやAutomator Launcherを通じてAppleScriptからAutomator Workflowを起動することができなくなってしまいました。セキュリティ向上のために取った策が裏目に出たと言われています。

一度、ヘルプビューワーのURLプロトコルがセキュリティホールになったことがあり、その直後からURLプロトコルが徹底的に塞がれて、URLプロトコル経由でアプリケーションのコントロールを行うと、実行が途中で止められたりしました。そのぐらいの時期にAutomator WorkflowのASからの実行がおかしくなった記憶があります。

AppleScriptからAutomatorのWorkflowを呼び出すケースはそれほどありませんが、まったくないわけでもありません(Excel書類にAutomator WorkflowやAppleScriptを埋め込んで実行したりしました。あとよく使うのはPDFの連結)。そこで、Mac OS X 10.6上でAutomator Workflowを呼び出す実験を行い、再利用できるようにルーチンにまとめておきました。

スクリプト名:指定のAutomator Workflowのファイルを実行する v3
set aWorkflow to choose file
set aRes to executeAutomatorWorkflow(aWorkflow) of me

–指定のAutomator Workflowのファイルを実行する
on executeAutomatorWorkflow(aWorkflow)
  set aShell to “/usr/bin/automator “ & (quoted form of POSIX path of aWorkflow)
  
with timeout of 3600 seconds
    try
      set aRes to (do shell script aShell)
    on error errorMes
      return errorMes
    end try
  end timeout
  
  
return aRes
end executeAutomatorWorkflow

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

04/05 指定のAutomator Workflowのファイルを実行する

任意のAutomator WorkflowをAutomator上で実行します。AppleScriptの処理の中でAutomatorのWorkflowを呼び出せると大幅に手抜きができるケースがあります。また、AppleScript側に機能が解放されていないのになぜかAutomator上では使えるようになっている機能を利用したい、といったケースにも役立つことでしょう。

スクリプト名:指定のAutomator Workflowのファイルを実行する
set aWorkflow to choose file
executeAutomatorWorkflow(aWorkflow) of me

指定のAutomator Workflowのファイルを実行する
on executeAutomatorWorkflow(aWorkflow)
  try
    with timeout of 3600 seconds
      using terms from applicationAutomator
        tell applicationAutomator
          close every workflow saving no
          
open aWorkflow
          
tell workflow 1
            execute
          end tell
        end tell
      end using terms from
    end timeout
  on error
    display dialogAutomator Workflow実行時にエラーが発生しましたbuttons {”OK“} default button 1 with icon caution
  end try
end executeAutomatorWorkflow

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

03/09 Automator集計

AppleScriptでプログラムを作るということは、スクリプティング対象となるアプリケーションについて、ひいてはMacのアプリケーション全般に対する情報収集が必要になります。とくに、OSのバージョンが上がった直後は、どのような変更が加わったのかをOS本体のみならず、添付アプリケーションについても調査することになります。中でも、Automatorのアクションがどの程度充実しているか、というのは個人的に大きな関心事なので……このようなプログラムを組んで調査することになります。これは、そういうために作ったAppleScriptです。

スクリプト名:Automator集計
【コメント】 Automatorのターゲットアプリケーション別集計
ただし、1つのActionが複数のターゲットを指定していることもある。

tell applicationAutomator
  set a to target application of every Automator action
end tell

set a_ref to a reference to a

set aList to {}
set aList_ref to a reference to aList

set bList to {}
set bList_ref to a reference to bList

repeat with i in a_ref
  repeat with j in i
    set aGenle to contents of j
    
if aGenle is not in aList_ref then
      set the end of aList to aGenle
      
set the end of bList to 1
    else
      repeat with k from 1 to length of bList_ref
        if contents of item k of aList = aGenle then
          exit repeat
        end if
      end repeat
      
      
set item k of bList_ref to (item k of bList_ref) + 1
    end if
  end repeat
end repeat

set eList to {}
repeat with i from 1 to (length of aList)
  set the end of eList to {item i of aList_ref, item i of bList_ref}
end repeat

eList

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に