-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCreateGridset.py
143 lines (115 loc) · 4.97 KB
/
CreateGridset.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import zipfile
import os
import re
import shutil
import traceback
# Consistent log file path in the desired directory
log_path = os.path.join(
os.environ.get("APPDATA", ""),
"Ace Centre",
"AACSpeakHelper",
"creategridset_log.txt",
)
def write_log(message):
"""Writes a message to the log file."""
with open(log_path, "a") as log_file:
log_file.write(f"{message}\n")
def create_folder_shortcut(target_folder, shortcut_name):
try:
desktop = os.path.join(os.path.join(os.environ["USERPROFILE"]), "Desktop")
shortcut_path = os.path.join(desktop, f"{shortcut_name}.lnk")
write_log(f"Creating shortcut at: {shortcut_path}")
vbs_script = f"""
Set oWS = WScript.CreateObject("WScript.Shell")
sLinkFile = "{shortcut_path}"
Set oLink = oWS.CreateShortcut(sLinkFile)
oLink.TargetPath = "{target_folder}"
oLink.Save
"""
vbs_script_path = os.path.join(desktop, "create_shortcut.vbs")
with open(vbs_script_path, "w") as file:
file.write(vbs_script)
write_log(f"VBS script created at: {vbs_script_path}")
result = os.system(f'cscript //Nologo "{vbs_script_path}"')
write_log(f"Executed VBS script with result: {result}")
os.remove(
vbs_script_path
) # Clean up the VBS script file after creating the shortcut
write_log("VBS script removed successfully.")
except Exception as e:
write_log(f"Failed to create shortcut: {e}")
write_log(traceback.format_exc())
def modify_gridset(gridset_path, LocalAppPath):
if not os.path.exists(gridset_path):
write_log(f"Error: The gridset file does not exist: {gridset_path}")
return # Exit the function if the file doesn't exist
try:
temp_dir = "temp_gridset"
os.makedirs(temp_dir, exist_ok=True)
write_log(f"Created temporary directory: {temp_dir}")
with zipfile.ZipFile(gridset_path, "r") as zip_ref:
zip_ref.extractall(temp_dir)
write_log(f"Extracted gridset: {gridset_path}")
for foldername, _, filenames in os.walk(temp_dir):
for filename in filenames:
if filename.endswith(".xml"):
xml_path = os.path.join(foldername, filename)
with open(xml_path, "r") as f:
filedata = f.read()
local_app_data_path = os.environ.get("LOCALAPPDATA", "")
full_path_to_exe = os.path.join(
local_app_data_path,
"Programs",
"Ace Centre",
"AACSpeakHelper",
"client.exe",
)
full_path_to_exe_escaped = full_path_to_exe.replace("\\", "\\\\")
new_data = re.sub(
"%FILEPATHTOREPLACE%", full_path_to_exe_escaped, filedata
)
with open(xml_path, "w") as f:
f.write(new_data)
write_log(f"Modified XML file: {xml_path}")
# Updated to keep everything under Ace Centre\AACSpeakHelper
new_gridset_dir = os.path.join(
LocalAppPath, "Ace Centre", "AACSpeakHelper", "Example AAC Helper Pages"
)
new_gridset_path = os.path.join(new_gridset_dir, "AAC Helper Tool Demo.gridset")
os.makedirs(new_gridset_dir, exist_ok=True)
write_log(f"Created new gridset directory: {new_gridset_dir}")
with zipfile.ZipFile(new_gridset_path, "w") as zipf:
for root, _, files in os.walk(temp_dir):
for file in files:
zipf.write(
os.path.join(root, file),
os.path.relpath(os.path.join(root, file), temp_dir),
)
write_log(f"Created new gridset file: {new_gridset_path}")
# Clean up
shutil.rmtree(temp_dir)
write_log(f"Removed temporary directory: {temp_dir}")
# Prevent deleting the original file
# os.remove(gridset_path)
write_log(f"Retained original gridset file: {gridset_path}")
# Create a shortcut on the Desktop
create_folder_shortcut(new_gridset_dir, "Example AAC Pages")
except Exception as e:
write_log(f"Error during modify_gridset: {e}")
write_log(traceback.format_exc())
if __name__ == "__main__":
try:
write_log("Script started.")
app_data_path = os.environ.get("APPDATA", "")
gridset_location = os.path.join(
app_data_path,
"Ace Centre",
"AACSpeakHelper",
"TranslateAndTTS DemoGridset.gridset",
)
write_log(f"Gridset location: {gridset_location}")
modify_gridset(gridset_location, app_data_path)
write_log("Script completed successfully.")
except Exception as e:
write_log(f"Critical error in main execution: {e}")
write_log(traceback.format_exc())