-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00 Import scripts as links.applescript
220 lines (161 loc) · 6.28 KB
/
00 Import scripts as links.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
-- @description Create cues to run external scripts in Qlab
-- @author Ben Smith
-- @link bensmithsound.uk
-- @version 1.0
-- @testedmacos 10.13.6
-- @testedqlab 4.6.9
-- @about Run this script in MacOS's "Script Editor" to quickly create script cues using the scripts in this repository. As of Qlab 4.6.9 you cannot set "run in separate process" through applescript. Input your default, and the script will alert you if you need to change it.
-- @separateprocess TRUE
-- RUN SCRIPT -----------------------------
-- Declarations
use framework "Foundation"
use framework "OSAKit"
use scripting additions
-- Get user input: scripts to generate cues for
set scriptFiles to choose file with prompt "Please select the scripts to import" of type {"applescript"} with multiple selections allowed
-- Repeat with each selected script
repeat with eachScript in scriptFiles
-- Compile script
set aURL to (current application's |NSURL|'s fileURLWithPath:(POSIX path of eachScript))
set destinationURL to (aURL's URLByDeletingPathExtension()'s URLByAppendingPathExtension:"scpt")
-- Create a list of each line of the script
set eachScriptContents to paragraphs of (read eachScript)
set scriptContents to ""
log "-----------"
set userDefinedVariables to false
repeat with eachLine in eachScriptContents
-- Get tags
if eachLine contains "@description" then
set eachScriptDescription to trimLine(eachLine, "-- @description ", 0)
log "Description: " & eachScriptDescription
else if eachLine contains "@author" then
set eachScriptAuthor to trimLine(eachLine, "-- @author ", 0)
log "Author: " & eachScriptAuthor
else if eachLine contains "@source" then
set eachScriptSource to trimLine(eachLine, "-- @source ", 0)
log "Source: " & eachScriptSource
else if eachLine contains "@version" then
set eachScriptVersion to trimLine(eachLine, "-- @version ", 0)
log "Version: " & eachScriptVersion
else if eachLine contains "@testedmacos" then
set eachScriptMacOS to trimLine(eachLine, "-- @testedmacos ", 0)
log "MacOS: " & eachScriptMacOS
else if eachLine contains "@testedqlab" then
set eachScriptQlab to trimLine(eachLine, "-- @testedqlab ", 0)
log "Qlab: " & eachScriptQlab
else if eachLine contains "@about" then
set eachScriptAbout to trimLine(eachLine, "-- @about ", 0)
log "About: " & eachScriptAbout
else if eachLine contains "@separateprocess" then
set eachScriptSeparateProcess to trimLine(eachLine, "-- @separateprocess ", 0)
log "Separate Process: " & eachScriptSeparateProcess
end if
if eachLine contains "USER DEFINED VARIABLES ---" then
set userDefinedVariables to true
set userDefinedVariablesContent to ""
else if eachLine contains "--- END OF USER DEFINED VARIABLES" then
set userDefinedVariablesContent to userDefinedVariablesContent & "
" & eachLine
set userDefinedVariables to false
end if
if userDefinedVariables is true then
set userDefinedVariablesContent to userDefinedVariablesContent & "
" & eachLine
set eachLine to ""
end if
-- Get script source
if eachLine does not contain "-- @" and eachLine does not contain "-- " then
set scriptContents to scriptContents & "
" & eachLine
end if
set scriptContents to my trimLine(scriptContents, "
", 0)
end repeat
try
if eachScriptSeparateProcess is "FALSE" then return -- scripts need to work as a separate process for this method
end try
set {theScript, theError} to (current application's OSAScript's alloc()'s initWithContentsOfURL:aURL |error|:(reference))
if theScript is missing value then error theError's |description|() as text
set {theResult, theError} to (theScript's compileAndReturnError:(reference))
if theResult as boolean is false then return theError's |description|() as text
set {theResult, theError} to (theScript's writeToURL:destinationURL ofType:"scpt" usingStorageOptions:0 |error|:(reference))
if theResult as boolean is false then return theError's |description|() as text
set newPathAlias to destinationURL as alias
set newPath to POSIX path of newPathAlias
log newPath
tell application id "com.figure53.Qlab.4" to tell front workspace
-- Get cue to write, or create cue
set selectedCues to (selected as list)
if (length of scriptFiles is 1) and (length of selectedCues is 1) and (q type of item 1 of selectedCues is "Script") then
set scriptCue to last item of (selected as list)
else
make type "Script"
set scriptCue to last item of (selected as list)
end if
-- Set cue name
try
set q name of scriptCue to eachScriptDescription
on error
tell application "System Events"
set cueName to name of eachScript
set cueName to my trimLine(cueName, ".applescript", 1)
end tell
set q name of scriptCue to cueName
end try
-- Set cue note
try
set cueNote to eachScriptAbout
try
set cueNote to cueNote & " (" & eachScriptAuthor
try
set cueNote to cueNote & " // " & eachScriptSource & ")"
on error
set cueNote to cueNote & ")"
end try
end try
set notes of scriptCue to cueNote
end try
-- Set script source
set newScriptSource to "set theScript to load script \"" & (newPath) & "\"
run theScript"
try
set script source of scriptCue to "-- @version " & eachScriptVersion & "
"
on error
set script source of scriptCue to ""
end try
set script source of scriptCue to script source of scriptCue & userDefinedVariablesContent & "
" & newScriptSource
end tell
end repeat
-- FUNCTIONS ------------------------------
on trimLine(theText, trimChars, trimIndicator)
-- trimIndicator options:
-- 0 = beginning
-- 1 = end
-- 2 = both
set x to the length of the trimChars
---- Trim beginning
if the trimIndicator is in {0, 2} then
repeat while theText begins with the trimChars
try
set theText to characters (x + 1) thru -1 of theText as string
on error
-- if the text contains nothing but the trim characters
return ""
end try
end repeat
end if
---- Trim ending
if the trimIndicator is in {1, 2} then
repeat while theText ends with the trimChars
try
set theText to characters 1 thru -(x + 1) of theText as string
on error
-- if the text contains nothing but the trim characters
return ""
end try
end repeat
end if
return theText
end trimLine