2015-02-17 34 views
0

我的编码知识是有限的。我正在尝试使用applescript和Keynote将PPT转换为HTML的过程自动化。在这种page我发现下面的非工作的苹果脚本:Applescript - 批量转换PPT到HTML

-- THE DESTINATION FOLDER 
-- (see the "path" to command in the Standard Additions dictionary for other locations, such as pictures folder, movies folder, sites folder, desktop folder) 
set the defaultDestinationFolder to (path to documents folder) 

tell application "Keynote" 
activate 
try 
    if playing is true then tell the front document to stop 

    if not (exists document 1) then error number -128 

    -- DERIVE NAME FOR NEW FOLDER FROM NAME OF THE FRONT DOCUMENT 
    set documentName to the name of the front document 
    if documentName ends with ".key" then ¬ 
     set documentName to text 1 thru -5 of documentName 

    -- CREATE AN EXPORT DESTINATION FOLDER 
    -- IMPORTANT: IT’S ADVISED TO ALWAYS CREATE A NEW DESTINATION FOLDER, AS THE CONTENTS OF ANY TARGETED FOLDER WILL BE OVERWRITTEN 
    tell application "Finder" 
     set newFolderName to documentName 
     set incrementIndex to 1 
     repeat until not (exists folder newFolderName of defaultDestinationFolder) 
      set newFolderName to documentName & "-" & (incrementIndex as string) 
      set incrementIndex to incrementIndex + 1 
     end repeat 
     set the targetFolder to ¬ 
      make new folder at defaultDestinationFolder with properties ¬ 
       {name:newFolderName} 
     set the targetFolderHFSPath to targetFolder as string 
    end tell 

    -- EXPORT THE DOCUMENT 
    with timeout of 1200 seconds 
     export front document as HTML to file targetFolderHFSPath 
    end timeout 

    on error errorMessage number errorNumber 
    display alert "EXPORT PROBLEM" message errorMessage 
    error number -128 
    end try 
    end tell 

    -- OPEN THE DESTINATION FOLDER 
    tell application "Finder" 
open the targetFolder 
end tell 

-- VIEW THE PRESENTATION 
tell application "Safari" 
activate 
open file (targetFolderHFSPath & "index.html") 
end tell 

我正在寻找一种方式来解决这个问题。目前,我得到以下结果:

error "Keynote got an error: No user interaction allowed." number -1713 

回答

0

你破坏了苹果的基本规则之一。避免将告诉块放在告诉块内。在你的情况下,你把一个Finder告诉块代码在Keynote中。这可能会导致冲突,从而导致错误。我认为这是你的问题。

尝试对分离告诉块...

-- THE DESTINATION FOLDER 
-- (see the "path" to command in the Standard Additions dictionary for other locations, such as pictures folder, movies folder, sites folder, desktop folder) 
set the defaultDestinationFolder to (path to documents folder) 


try 
    tell application "Keynote" 
     activate 
     if playing is true then tell the front document to stop 

     if not (exists document 1) then error number -128 

     -- DERIVE NAME FOR NEW FOLDER FROM NAME OF THE FRONT DOCUMENT 
     set documentName to the name of the front document 
     if documentName ends with ".key" then ¬ 
      set documentName to text 1 thru -5 of documentName 

     -- CREATE AN EXPORT DESTINATION FOLDER 
     -- IMPORTANT: IT’S ADVISED TO ALWAYS CREATE A NEW DESTINATION FOLDER, AS THE CONTENTS OF ANY TARGETED FOLDER WILL BE OVERWRITTEN 
    end tell 

    tell application "Finder" 
     set newFolderName to documentName 
     set incrementIndex to 1 
     repeat until not (exists folder newFolderName of defaultDestinationFolder) 
      set newFolderName to documentName & "-" & (incrementIndex as string) 
      set incrementIndex to incrementIndex + 1 
     end repeat 
     set the targetFolder to ¬ 
      make new folder at defaultDestinationFolder with properties ¬ 
       {name:newFolderName} 
     set the targetFolderHFSPath to targetFolder as string 
    end tell 

    -- EXPORT THE DOCUMENT 
    with timeout of 1200 seconds 
     tell application "Keynote" 
      export front document as HTML to file targetFolderHFSPath 
     end tell 
    end timeout 

on error errorMessage number errorNumber 
    display alert "EXPORT PROBLEM" message errorMessage 
    error number -128 
end try 

-- OPEN THE DESTINATION FOLDER 
tell application "Finder" 
    open the targetFolder 
end tell 

-- VIEW THE PRESENTATION 
tell application "Safari" 
    activate 
    open file (targetFolderHFSPath & "index.html") 
end tell