-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAppleScript (Binary).py
160 lines (124 loc) · 4.95 KB
/
AppleScript (Binary).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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""
This borrows heavily from the BinaryPlist package
https://github.com/tyrone-sudeium/st3-binaryplist
"""
import sublime
from sublime import Region
import sublime_plugin
from sublime_plugin import EventListener
from sublime_plugin import TextCommand
import os
import re
import platform
import subprocess
# GLOBAL STUFF
saved_cursor_data = []
SYNTAX_FILE = "Packages/AppleScript Extensions/AppleScript (Binary).sublime-syntax"
END_REGEX = r"f\s?a\s?d\s?e\s?d\s?e\s?a\s?d\s?\Z"
class SaveCursorPositionsCommand(sublime_plugin.TextCommand):
def run(self, edit):
global saved_cursor_data
selections = self.view.sel()
if any(region.size() > 0 for region in selections):
# Save selections
saved_cursor_data = [(region.a, region.b) for region in selections]
else:
# Save cursor positions
saved_cursor_data = [(region.a,) for region in selections]
class RestoreCursorPositionsCommand(sublime_plugin.TextCommand):
def run(self, edit):
global saved_cursor_data
if saved_cursor_data:
self.view.sel().clear()
for data in saved_cursor_data:
if len(data) == 2:
# Restore selection
self.view.sel().add(sublime.Region(data[0], data[1]))
else:
# Restore cursor position
self.view.sel().add(sublime.Region(data[0]))
def is_syntax_set(view=None):
if view is None:
view = sublime.active_window().active_view()
return "AppleScript (Binary).sublime-syntax" in view.settings().get("syntax")
def is_binary(view):
selection = view.substr(Region(0, view.size()))
return re.search(END_REGEX, selection)
class ScptBinaryCommand(EventListener):
def on_load(self, view):
# Check if binary, convert to plain-text, mark as "was binary"
if is_binary(view):
view.run_command("binary_toggle")
def on_post_save(self, view):
# Convert back to plain-text
if view.get_status("is_binary"):
view.run_command("save_cursor_positions")
view.run_command("binary_toggle", {"force_to": True})
view.run_command("restore_cursor_positions")
def on_new(self, view):
pass
def on_clone(self, view):
pass
def on_pre_close(self, view):
pass
def on_close(self, view):
pass
def on_pre_save(self, view):
pass
def on_modified(self, view):
freshly_written = view.settings().get("freshly_written")
if freshly_written and is_binary(view):
view.run_command("save_cursor_positions")
view.run_command("binary_toggle")
view.run_command("restore_cursor_positions")
view.settings().erase("freshly_written")
def on_activated(self, view):
pass
class BinaryToggleCommand(TextCommand):
def decode_script(self, edit, view):
"""Reads in the view's file, converts it to plain and replaces the view's
buffer with the plain-text."""
file_name = view.file_name()
if (
file_name
and file_name != ""
and os.path.isfile(file_name) == True
and file_name.endswith(".scpt")
):
cmd = ["osadecompile", file_name]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
full_text, err = p.communicate()
view.set_encoding("UTF-8")
view.replace(edit, Region(0, view.size()), str(full_text.decode("utf-8")))
view.end_edit(edit)
view.set_status("is_binary", "Decompiled File")
view.set_scratch(True)
def encode_script(self, view):
"""Converts the view's plain-text back to a binary script and writes it out
to the view's file."""
file_name = view.file_name()
if file_name and file_name != "" and os.path.isfile(file_name) == True:
bytes = view.substr(Region(0, view.size())).encode("utf-8").rstrip()
try:
with open(file_name, "wb") as f:
f.write(bytes)
cmd = ["osacompile", "-o", file_name, file_name]
p = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
out, err = p.communicate()
view.settings().set("freshly_written", True)
view.sel().clear()
except Exception as e:
sublime.error_message(str(e))
raise e
def run(self, edit, force_to=False):
if platform.system() != "Darwin":
sublime.error_message("Binary AppleScript can only be edited on macOS")
return
if is_binary(self.view) and not force_to:
self.decode_script(edit, self.view)
if not is_syntax_set(self.view):
self.view.set_syntax_file(SYNTAX_FILE)
else:
self.encode_script(self.view)