Hello,
I’m trying to write a script it takes a bunch of either xlsx, docx or pptx files, and converts each to a PDF using the appropriate iWork app (Numbers, Pages and Keynote, respectively). Here’s how it goes:
- Files are dropped on to the script, or sent to it by Finder selection.
- Each file is put in a JSON-like record, with the designated target app as a property.
- Records are sorted (thanks, Nigel!) by the target app property, to make conversions more streamlined and to be able to close the target app once it’s done.
- Everything works well, except for this very specific segment:
export doc_id ¬
to file ((source_parent & source_name & ".pdf") as text) ¬
as PDF
It doesn’t even let me compile the script. If I put this block in and if-else clause, and based on the extension, explicitly refer to the app to handle the export, the script works well, e.g.:
if source_ext = "xlsx" or source_ext = "xls" then
tell app "Numbers" to export doc_id --...
It has been driving me crazy! Help would be extremely appreciated.
Thanks!
Here’s the full script:
tell application "Finder"
set thePaths to selection as alias list
end tell
set docs_to_process to {}
repeat with thePath in thePaths
tell application "Finder"
set source_parent to folder of thePath as text
set source_path to thePath
set source_ext to name extension of thePath
set source_name to (name of thePath as text)
end tell
if source_ext = "xlsx" or source_ext = "xls" then
set target_app to "Numbers"
else if source_ext = "ppt" or source_ext = "pptx" then
set target_app to "Keynote"
else if source_ext = "docx" or source_ext = "doc" then
set target_app to "Pages"
end if
set docs_to_process to docs_to_process & [{source_path:source_path, source_parent:source_parent, target_app:target_app, source_name:source_name}]
end repeat
customBubbleSort(docs_to_process, byTargetApp)
repeat with the_doc in docs_to_process
if application target_app is running then
set quitApp to false
else
set quitApp to true
end if
tell application target_app
set doc_id to open thePath -- gives the the opened doc a 'document id'
tell application "Finder"
if (exists file (source_parent & source_name & ".pdf")) then
set n to 1
repeat while (exists file (source_parent & source_name & ".pdf"))
set source_name to source_name & space & n
set n to n + 1
end repeat
end if
end tell
--export doc_id ¬
--to file ((source_parent & source_name & ".pdf") as text) ¬
--as PDF
close doc_id
if quitApp is true then
quit
end if
end tell
end repeat
on customBubbleSort(thelist, compObj)
script o
property lst : thelist
end script
repeat with i from (count thelist) to 2 by -1
set a to beginning of o's lst
repeat with j from 2 to i
set b to item j of o's lst
if (compObj's isGreater(a, b)) then
set item (j - 1) of o's lst to b
set item j of o's lst to a
else
set a to b
end if
end repeat
end repeat
end customBubbleSort
script byTargetApp
on isGreater(a, b)
(a's target_app > b's target_app)
end isGreater
end script