forked from gitmalong/pages2docx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpages2docx.applescript
81 lines (61 loc) · 2.46 KB
/
pages2docx.applescript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
on run inputFolder
-- Define a list to hold the names of files that could not be processed
set unprocessedFiles to {}
-- Define a list to hold the names of files that were processed successfully
set processedFiles to {}
tell application "Finder" to set theFiles to every file in the entire contents of folder inputFolder
repeat with theFile in theFiles
set fExt to name extension of theFile as text
set fName to name of theFile as text
set fDir to folder of theFile as text
if fExt is "pages" then
using terms from application "Pages"
convert(fDir, fName, "Pages", Microsoft Word, ".docx", unprocessedFiles, processedFiles)
end using terms from
end if
end repeat
-- Print the list of unprocessed files at the end of the script
log "Unprocessed files:\n" & my join(unprocessedFiles, "\n")
-- Print the list of successfully processed files at the end of the script
log "Successfully processed files:\n" & my join(processedFiles, "\n")
end run
on convert(dirName, fileName, appName, exportFormat, exportExtension, unprocessedFiles, processedFiles)
tell application appName
set fullPath to (dirName & fileName)
set posixFullPath to POSIX path of fullPath
set exportFileName to (dirName & fileName & exportExtension) as text
try
set doc to open fullPath
if doc is missing value then error "Document is missing"
set docName to name of doc
close access (open for access exportFileName)
if appName is "Pages" then
tell application "Pages"
try
export doc to file exportFileName as exportFormat
-- If the export succeeds, add the full POSIX path to the list of processed files
set end of processedFiles to posixFullPath
tell application "Finder"
move file exportFileName to folder dirName with replacing
end tell
on error
-- If an error occurs, add the full POSIX path to the list of unprocessed files
set end of unprocessedFiles to posixFullPath
end try
end tell
end if
close doc
on error
-- If an error occurs during opening, add the full POSIX path to the list of unprocessed files
set end of unprocessedFiles to posixFullPath
end try
end tell
end convert
on join(theList, theDelimiter)
set theString to ""
set theOldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelimiter
set theString to theList as string
set AppleScript's text item delimiters to theOldDelimiters
return theString
end join