2011-08-15 29 views
2

私は、Adobe Acrobatを使用してPDFファイルのOCRを実行する素晴らしいAppleScriptドロップレットを持っています。私はかなり良いPythonプログラマーですが、AppleScriptを本当に理解していません。私はOCRする必要がある私のシステム上のすべてのPDFのリストを持っています。スクリプトの一番上をそれぞれドラッグする必要があるのは本当に迷惑でしょう。私はドロップレットの各スクリプトを処理する小さなPythonプログラムを持っているか、そうでなければスクリプトを修正してテキストファイルを読み込んでドロップするようにしたいと思っています。AppleScriptのドロップレットを異なるファイル名で繰り返し呼び出すにはどうすればよいですか?

私は、単一のテストでPDFファイルを開くにはosascriptを使用してみました:

tell application "OCRIt-Acrobat" 
    open alias "imac3:Users:vy32:FFJ.pdf" 
end tell 

そして、私はこの素敵なエラーました:まあ、それはあまりにも便利ではありません

31:103: execution error: OCRIt-Acrobat got an error: alias "imac3:Users:vy32:FFJ.pdf" of «script» doesn’t understand the open message. (-1708) 

を。

私は何をすべきか知っていますか?ここで

は、そのすべての栄光に、OCRIt-のAcrobatです:

property mytitle : "ocrIt-Acrobat" 
-- Modified from a script created by Macworld http://www.macworld.com/article/60229/2007/10/nov07geekfactor.html 

-- I am called when the user open the script with a double click 
on run 
    tell me 
     activate 
     display dialog "I am an AppleScript droplet." & return & return & "Please drop a bunch of PDF files onto my icon to batch OCR them." buttons {"OK"} default button 1 with title mytitle with icon note 
    end tell 
end run 

-- I am called when the user drops Finder items onto the script icon 
-- Timeout of 36000 seconds to allow for OCRing really big documents 
on open droppeditems 
    with timeout of 36000 seconds 
     try 
      repeat with droppeditem in droppeditems 
       set the item_info to info for droppeditem 
       tell application "Adobe Acrobat Pro" 
        activate 
        open droppeditem 
       end tell 
       tell application "System Events" 

        tell application process "Acrobat" 

         click the menu item "Recognize Text Using OCR..." of menu 1 of menu item "OCR Text Recognition" of the menu "Document" of menu bar 1 
         try 
          click radio button "All pages" of group 1 of group 2 of group 1 of window "Recognize Text" 
         end try 
         click button "OK" of window "Recognize Text" 

        end tell 

       end tell 
       tell application "Adobe Acrobat Pro" 
        save the front document with linearize 
        close the front document 
       end tell 
      end repeat 
      -- catching unexpected errors 
     on error errmsg number errnum 
      my dsperrmsg(errmsg, errnum) 
     end try 
    end timeout 
end open 

-- I am displaying error messages 
on dsperrmsg(errmsg, errnum) 
    tell me 
     activate 
     display dialog "Sorry, an error occured:" & return & return & errmsg & " (" & errnum & ")" buttons {"Never mind"} default button 1 with icon stop with title mytitle 
    end tell 
end dsperrmsg 

ありがとう!

答えて

0

aliasを呼び出すと、その正確なパスで直接ファイルを呼び出しています。エイリアスが見つからない場合、Applescriptはエラーをスローします。テキストファイルからリストを読み取っている場合、チェックしていないリストの生成にエラーが発生する可能性があります。最低でも、あなたが有効なファイルで作業していることを確認するためにシステムイベントを使用する必要があります。

on FileExists(theFile) -- (String) as Boolean 
    tell application "System Events" 
     if exists file theFile then 
      return true 
     else 
      return false 
     end if 
    end tell 
end FileExists 

私は、液滴にドロップされたファイルやフォルダの任意の数を処理するために使用し、このテンプレートを持っています。限り、すべてのターゲット・ファイルが同じフォルダ階層にあるとして、あなたはファイルの外部リストは必要ありません。

property kTargetFileExtensions : {"txt", "rtf", "pdf"} 
property pValidFileList : {} 

on open of theFiles -- Executed when files or folders are dropped on the script 

    set fileCount to (get count of items in theFiles) 

    repeat with thisFile from 1 to fileCount 
     set theFile to item thisFile of theFiles 

     my processInitialFile(theFile) 

    end repeat 

    my processValidFileList() 

end open 

on run {} -- Executed when the script is run from within the editor 
    set sourceFolder to (choose folder) 

    my processInitialFile(sourceFolder) 

    my processValidFileList() 
end run 

on processInitialFile(theFile) 
    tell application "System Events" 
     set file_info to get info for theFile 
    end tell 

    if visible of file_info is true then -- check for the file extension here as well 
     if folder of file_info is true then 
      my createList(theFile) 
     else 
      set targetFileFound to isTargetFile(fileName, kTargetFileExtensions) of me 

      if (targetFileFound) then 
       set end of pValidFileList to theFile 
      end if 
     end if 
    end if 
end processInitialFile 

on processValidFileList() -- (void) as void 
    set firstFile to 1 
    set lastFile to (count pValidFileList) 
    repeat with thisFile from firstFile to lastFile 
     set theFile to item thisFile of pValidFileList 

     log theFile 

     (* enter file processing code here. *) 

    end repeat 

end processValidFileList 

on createList(mSource_folder) 
    set item_list to "" 

    tell application "System Events" 
     set item_list to get the name of every disk item of (mSource_folder as alias) 
    end tell 

    set item_count to (get count of items in item_list) 

    repeat with i from 1 to item_count 
     set the_properties to "" 

     set the_item to item i of the item_list 
     set fileName to the_item 
     set the_item to ((mSource_folder & the_item) as string) as alias 

     tell application "System Events" 
      set file_info to get info for the_item 
     end tell 

     if visible of file_info is true then -- check for the file extension here as well 
      if folder of file_info is true then 
       my createList(the_item) 
      else 
       set targetFileFound to isTargetFile(fileName, kTargetFileExtensions) of me 

       if (targetFileFound) then 
        set end of pValidFileList to the_item 
       end if 
      end if 
     end if 

    end repeat 
end createList 

on isTargetFile(theFilename, theTargetExtensions) -- (string, array) as boolean 
    set AppleScript's text item delimiters to "." 
    set fileNameList to every text item of theFilename 
    set AppleScript's text item delimiters to "" 

    try 
     set theFileExtension to item 2 of fileNameList as string 
    on error 
     return false 
    end try 

    set firstTargetExtension to 1 
    set lastTargetExtension to (count theTargetExtensions) 
    repeat with thisTargetExtension from firstTargetExtension to lastTargetExtension 
     set targetExtension to item thisTargetExtension of theTargetExtensions 
     if theFileExtension is targetExtension then 
      return true 
     end if 
    end repeat 

    return false 
end isTargetFile 
+0

ありがとうございました。ファイルはすべて別のフォルダにありますが、すべてのファイルにシンボリックリンクを持つ単一のフォルダを作成することができます。 (これは私には以前は起こりませんでした)。 '/ real/file/names/with/slashes'の上にスクリプトを書いてもいいですか、あるいは::Crazy:AppleScript:Filenames:With:Colons'を与える必要がありますか? – vy32

+0

AppleScriptはHFSを必要とします( ':Crazy:AppleScript:ファイル名:with:コロン') –

+0

Ugh。これは狂った言葉です。 Pythonでファイルを開く簡単な方法があることを願っています... – vy32

0

あなたは、ファイルを開くためのAppleScriptアプリケーションを伝えることはできません。これはアプリケーションであっても、スクリプトがどのように動作するかではありません。これを行うには、「スクリプトを実行」コマンドを使用し、そのコマンドでパラメータを渡すことができます。一例として、このアプリケーションのドロップレットを作成し、デスクトップに名前 "aaa"で保存します。

on run argList 
    try 
     -- when an application is double-clicked it errors seemingly because no arguments are passed. 
     -- class errors in this case so we can use that to set argList to {} so the script will work. 
     class of argList 
    on error 
     set argList to {} 
    end try 
    someSubroutine(argList) 
end run 

on open argList 
    someSubroutine(argList) 
end open 

on someSubroutine(argList) 
    set argListCount to count of argList 
    tell me 
     activate 
     display dialog "Count of arguments: " & argListCount 
    end tell 
end someSubroutine 

は次のように実行すると

set appPath to (path to desktop as text) & "aaa.app" 
run script file appPath with parameters {1, 2} 

"実行argListに上で" 使用されている...このスクリプトを作成し、それを実行します。あなたはその上に物事を落とすことができ、 "open argList"ハンドラが使用されます。再度ダブルクリックすると、 "on run argList"が使用されます。

これはあなたが望むようにできることを示しています。 「スクリプトを実行」コマンドを使用して、パラメータ内のファイルパスを渡すだけです。

関連する問題