2012-04-10 6 views
2

ゴール:古いClarisworksとAppleworksのドキュメントは何千ものPDFファイルにする必要があります。AppleScript:AppleWorksのPDFに再帰的に印刷するためのプログラミング

問題:スクリプトページはオプションではありません。それは確実にフォーマットを保持しません。 Appleworksである必要があります。このスクリプトは動作します。私が識別できない理由でファイルをスキップします。

set appleworksFolder to choose folder 
tell application "Finder" 
    set folderItems to (files of entire contents of appleworksFolder) 
    repeat with I from 1 to number of items in folderItems 

     set the_doc to item I of folderItems 
     set doc_name to name of the_doc as text 

      (* Some files are missing metatags and try to save as "Untitled Document", 
       this block ensures a file name is unique, later *) 

      tell application "Finder" 
      set the clipboard to doc_name & ".pdf" 
     end tell 


      (* Each file exists in a folder with a path.txt file that will later 
       be used to put the file back where it was originally stored prior 
       to this conversion process *) 

     if name of the_doc is not "path.txt" then 
      try 
       tell application "Finder" 

      (* Many files no longer have name extensions and appear as UNIX 
       executables if not repaired *) 

        try 
         set nmex to name extension of the_doc as text 
        on error 
         set nmex to "ok" 
        end try 

        if nmex is not "cwk" or "CWK" then 
         set the_doc_str to the_doc as text 
         set doc_path to POSIX path of the_doc_str 
         do shell script "mv '" & doc_path & "' " & "'" & doc_path & ".cwk'" 
        end if 
        delay 1 


      (* In case Appleworks hangs or otherwise bungs up, I force-quit 
       it at the end of the script; this ensures its closed before 
       it tries to open the next file *) 

        if (application process "Appleworks 6" of application "System Events" exists) then 
         do shell script "killall 'LaunchCFMApp'" 
         delay 1 
        end if 

        tell application "AppleWorks 6" 
         open the_doc 
        end tell 

      (* Some of the documents are huge, this delay gives the app time to load 
       since this is all GUI-scripted *) 

        delay 5 
        tell application process "Appleworks 6" of application "System Events" 

      (* This is where I think I am encountering problems; there are two 
       possible warnings that may or may not appear on opening the doc; 
       that Appleworks needs to append a version number to the file (if 
       its old) or that the file may be damaged and thus would need to be 
       skipped. I get system beeps sometimes in this part, but I don't know 
       why! *) 

         if (button "OK" of window 1 of application process "AppleWorks 6" of application "System Events" exists) then 
          delay 0.5 
          keystroke return 
          delay 0.5 
         end if 
         delay 2 
         if (button "OK" of window 1 of application process "AppleWorks 6" of application "System Events" exists) then 
          delay 0.5 
          keystroke return 
          delay 0.5 
         end if 
         delay 2 
      (* If the document loads, the Appleworks welcome pane won't be there; 
       this part of the script works flawlessly, when it happens. Sometimes 
       documents are outside of print margins, hence the press-ok-button 
       conditional *) 

         if not (window "Starting Points" of application process "AppleWorks 6" of application "System Events" exists) then 
          tell application process "Appleworks 6" of application "System Events" 
           keystroke "p" using command down 
           delay 1 
           click menu button "PDF" of window "Print" of application process "AppleWorks 6" of application "System Events" 
           delay 1 
           click menu item "Save as PDF…" of menu "PDF" of menu button "PDF" of window "Print" of application process "AppleWorks 6" of application "System Events" 
           delay 1 
           keystroke "v" using command down 
           click button "Save" of window "Save" of application process "AppleWorks 6" of application "System Events" 
           delay 8 
           keystroke "w" using command down 
           delay 0.5 
           if (button 1 of window 1 of application process "AppleWorks 6" of application "System Events" exists) then 
            delay 0.5 
            keystroke "d" using command down 
            delay 0.5 
           end if 
           delay 0.5 
          end tell 
         end if 
         do shell script "killall 'LaunchCFMApp'" 
         delay 3 
        end tell 
       end tell 
      end try 
     end if 
    end repeat 
end tell 

私はちょうど週末に何千ものファイルのカップルを介して実行してPDFを作成するには、この赤ちゃんにしたいが、私は一晩にそれを実行するたびに、私はスキップされたドキュメントの数百正しく処理された文書、数百または数千を見つけますそして、多くの場合、のプリントダイアログの印刷ダイアログが表示されます。これは明らかにAppleworksのコンテキスト外でCommand + Pを使用したことに由来します。私はApplescriptのnoobです、確かに、これは私を数週間運転してきました!

答えて

2

それは非常に特異的かつ仕立てであるように、これは、本当にあなたの問題を解決ではありませんが、私はいくつかの可能性の便利なヒントがあります:

  • をあなたは「スタートポイント」ウィンドウを閉じる必要はありません。 。 AppleWorksに文書を開くように指示すると、それは却下されます。

  • できるだけ擬似キーストロークを使用するようにしてください。たとえば、代わりに印刷ダイアログを開くためにcmd+Pをシミュレートするのではなく、そのFile、メニュー内のアプリケーションのPrint…コマンドのクリックをシミュレートした方がよい:

    click menu item "Print…" of menu "File" of menu bar 1 of application process "AppleWorks 6" 
    

    、代わりの文書を閉じますcmd+Wを行い、適切なあなただけの場合には、ちょうどtell app "AppleWorks 6" to activatekeystrokeコマンドの前にそれを活性化させることによって、彼らは正しいアプリケーションによって受信されることを保証する、キーストロークをシミュレートする必要がある場合の方法は、

    tell application "AppleWorks 6" to close front document saving no 
    
  • を行うことであることその時点で最前列のアプリではありませんでした。

  • if (button 1 of window 1 of (*...*) exists)を使用すると、「印刷」ダイアログが開いている間にマシンをチェックするなどの理由で、スクリプトが数分(ハングした)なぜなら、番号ではなく名前でウィンドウをアドレス指定することが常に推奨されるからです(window 1は単にフロントウィンドウですから、すぐに別のウィンドウになる可能性があります)。したがって、という名前のウィンドウとそのようなが存在するかどうかを確認する方が良いでしょう。

  • エラーが発生すると予測される何千ものファイルを処理するには、コードを再構成してエラー処理を再考する必要があります。たとえば、スキップされたファイルとその理由を確認するために、エラー情報をテキストファイルに記録することができます。

    set logfile to alias "Macintosh HD:Users:user:Desktop:errors.log" 
    open for access logfile with write permission 
    
    repeat with the_doc in folderItems 
        try 
         open_with_appleworks(the_doc) 
         print_to_pdf() 
         close_document_and_quit_appleworks() 
        on error error_message 
         write error_message & "\n" to logfile 
         close_document_and_quit_appleworks() 
        end try 
    end repeat 
    
    close access logfile 
    

    を同様の方法で、あなたも、成功のメッセージを記録することができます:あなたのメインループは次のようになりますので、エラー処理や詳細から別のプログラム・ロジックに、あなたは、ハンドラを使用することができます。シングルハンドラでは、各ステップを正確に行う方法の詳細を指定する必要があります。彼らはまた、ログに記録されるようにいくつかのエラー情報を提供することができます。

    to open_with_appleworks(the_doc) 
        tell app "AppleWorks 6" 
         try 
          (* do your stuff here *) 
         on error err_msg 
          (* re-signal the error to main loop! *) 
          error "Failed to open " & (the_doc as text) & err_msg 
         end try 
        end tell 
    end open_with_appleworks 
    
  • ハードコードされた遅延はかなりあなたのスクリプトを遅く:のみあなたの文書の一部を開くために数秒かかるかもしれないが、あなたのスクリプトをします常に 5秒待つ - ドキュメントが瞬時に開く場合でも。これは、ドキュメントがまだ開かれているかどうかをループチェックを持っている方が良いでしょう:

    repeat while not (exists front document) 
        -- just wait 
        -- or, delay 0.1 -- if you really want to 
    end repeat 
    

    彼らがなるのと同じで、それらのほとんどは回避することができ、ほぼすべての他の遅延を保持している実際にははるかに短い以上 。半分の時間を遅らせるのではなく、警告またはダイアログが現れたら、それが存在するかどうかを確認する方が良いでしょう。

  • (残念ながら1つは残念です)残念ながら、処理されるすべてのファイルを1つのフォルダにコピーするという問題を抱えているようで、いくつかの「path.txt」から来た... ファイルがあるフォルダのサブフォルダにあり、フィルタで見つけることができる場合は避けてください:その特定の検索(example)で「smsartフォルダ」を作成し、 AppleScript set theFiles to choose file with multiple selections allowed、その特定のスマートフォルダ内のすべてのファイルを選択し、あなたはいいです。このスクリプトは、からget parent of f(ファインダfile参照を返します。これはaliasPOSIX fileのアドレスに簡単に変換できます)を求めることによって、file fがどこにあるか簡単に調べることができます。次に、ドキュメントを「印刷」することによって生成されたPDFファイルの名前と場所を知っているので、そのPDFをソースドキュメントがあるフォルダに移動できます。

+0

これはちょうど私が探していたアドバイスの1つです。ノブとして、これらの提案を実装するのに時間がかかります。しかし、一般的には、これは私が望んでいたものです。このスクリプトは手で4000以上のファイルを開いてPDFに保存しなくても済むようにするための方法です。私にこのすべてを書く時間をとってくれたことに感謝します。 – Jonline

+0

私には1つの質問がありますが... Appleworksのエラーウィンドウにはタイトルがないようですが、システムのダイアログボックスを使用していないため、「ウィンドウ1」を使用していました。どのように私はこの情報を取得できますか? – Jonline

+2

ターゲットとしたいエラーウィンドウが表示されている状態で、アプリケーションプロセス「AppleWorks 6」のウィンドウのプロパティを取得するために、「アプリケーションにシステムイベントを通知する」小さなテストスクリプトを実行します。これにより、ウィンドウの辞書一覧が表示されます。たとえば、ダイアログウィンドウにはプロパティ 'description:"ダイアログがあります。これは後でチェックすることができます(例えば '存在するウィンドウ1とウィンドウ1の説明は"ダイアログ "です)。 – fanaugen

関連する問題