-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_replace.py
More file actions
executable file
·61 lines (46 loc) · 2.09 KB
/
export_replace.py
File metadata and controls
executable file
·61 lines (46 loc) · 2.09 KB
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
#!/usr/bin/env python3
import subprocess
import os
def export_and_replace_pdf():
"""
This function exports the currently active Word document to PDF.
It checks if a PDF already exists before creating one.
"""
applescript = '''
tell application "Microsoft Word"
if (count of documents) is 0 then
display dialog "No Word document is open!" buttons {"OK"} default button 1
return
end if
-- Get the document you're currently working on
set activeDoc to active document
-- Get the file path of this document
set docPath to full name of activeDoc as string
-- Convert Mac-style path to Unix-style path (POSIX)
set posixPath to POSIX path of docPath
-- Create the PDF filename by replacing .docx with .pdf
set pdfPath to (text 1 thru -6 of posixPath) & ".pdf"
-- Check if PDF already exists using shell command
set pdfExists to (do shell script "test -f " & quoted form of pdfPath & " && echo 'yes' || echo 'no'")
-- If PDF doesn't exist, show warning and stop
if pdfExists is "no" then
display dialog "Warning: No existing PDF found at:" & return & return & pdfPath & return & return & "Please create the PDF manually first, then use this script to update it." buttons {"OK"} default button 1 with icon caution
return
end if
-- Export the document
save as activeDoc file name pdfPath file format format PDF
display notification "PDF updated successfully!" with title "Word Export" sound name "Glass"
end tell
'''
try:
result = subprocess.run(
['osascript', '-e', applescript], # run osascript with this AppleScript code
capture_output=True,
text=True,
check=True
)
print("Export completed successfully!")
except subprocess.CalledProcessError as e:
print(f"Error occurred: {e.stderr}")
if __name__ == "__main__":
export_and_replace_pdf()