-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-generate-app-config.applescript
139 lines (102 loc) · 3.66 KB
/
2-generate-app-config.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
-- @description Create Open Stage Control server setup
-- @author Ben Smith
-- @link bensmithsound.uk
-- @version 2.0
-- @testedmacos 10.14.6
-- @testedqlab 4.6.10
-- @about Creates a file to open/load in Open Stage Control, with settings required to start the server.
-- @changelog
-- v2.0 + change filenames
---- DECLARATIONS -------------------------
use framework "Foundation"
use scripting additions
property ca : a reference to current application
property NSData : a reference to ca's NSData
property NSDictionary : a reference to ca's NSDictionary
property NSJSONSerialization : a reference to ca's NSJSONSerialization
property NSString : a reference to ca's NSString
property NSUTF8StringEncoding : a reference to 4
---- RUN SCRIPT ---------------------------
set qlabConfig to getConfig()
set configFile to ((getRootFolder() as text) & "qlab-display.config")
set configContents to "{"
if useTCP of control of qlabConfig is true then
set configContents to configContents & "\"tcp-port\":53001,\"tcp-targets\":[\""
set configContents to configContents & |ip| of QlabMain of qlabConfig & ":53000\""
if QlabCount of qlabConfig is 2 then
set configContents to configContents & ",\"" & |ip| of QlabBackup of qlabConfig & ":53000\""
end if
set configContents to configContents & "],"
end if
set rootFolder to getRootFolder()
set rootFolder to rootFolder as text
set rootFolder to POSIX path of alias rootFolder
set configContents to configContents & "\"load\":\"" & rootFolder & "qlab-display_layout.json\",\"custom-module\":\"" & rootFolder & "qlab-display_module.js\""
if useTCP of control of qlabConfig is false then
set configContents to configContents & ",\"osc-port\":53001"
end if
set configContents to configContents & "}"
writeToFile((configContents as text), configFile, false)
-- FUNCTIONS ------------------------------
on JSONtoRecord from fp
local fp
set JSONdata to NSData's dataWithContentsOfFile:fp
set [x, E] to (NSJSONSerialization's ¬
JSONObjectWithData:JSONdata ¬
options:0 ¬
|error|:(reference))
if E ≠ missing value then error E
tell x to if its isKindOfClass:NSDictionary then ¬
return it as record
x as list
end JSONtoRecord
on getConfig()
set qlabConfigFile to ((getRootFolder() as text) & "qlab-info-config.json")
set qlabConfigPOSIX to POSIX path of qlabConfigFile
set qlabConfig to JSONtoRecord from qlabConfigPOSIX
return qlabConfig
end getConfig
on getRootFolder()
set thePath to path to me
tell application "Finder"
set thePath to parent of thePath
end tell
end getRootFolder
on writeToFile(thisData, targetFile, appendData) -- (string, file path as string, boolean)
try
set the targetFile to the targetFile as text
set the openTargetFile to ¬
open for access file targetFile as «class furl» with write permission
if appendData is false then ¬
set eof of the openTargetFile to 0
write thisData to the openTargetFile starting at eof
close access the openTargetFile
return true
on error
try
close access file targetFile
end try
return false
end try
end writeToFile
on readFile(theFile)
try
set theFile to theFile as text
set fileContents to paragraphs of (read file theFile)
return fileContents
on error
return "error"
end try
end readFile
on splitString(theString, theDelimiter)
-- save delimiters to restore old settings
set oldDelimiters to AppleScript's text item delimiters
-- set delimiters to delimiter to be used
set AppleScript's text item delimiters to theDelimiter
-- create the array
set theArray to every text item of theString
-- restore old setting
set AppleScript's text item delimiters to oldDelimiters
-- return the array
return theArray
end splitString