-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApplescript Utilities.applescript
175 lines (145 loc) · 4.65 KB
/
Applescript Utilities.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
-- @description Applescript Utilities
-- @author Ben Smith
-- @link bensmithsound.uk
-- @version 1.0
-- @about Common functions to call for other scripts
-- @changelog
-- v1.0 + init
-- USER DEFINED VARIABLES -----------------
---------- END OF USER DEFINED VARIABLES --
-- VARIABLES FROM QLAB NOTES --------------
------------------ END OF QLAB VARIABLES --
---- RUN SCRIPT ---------------------------
-- FUNCTIONS ------------------------------
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
on findAndReplaceInText(theText, theSearchString, theReplacementString)
set AppleScript's text item delimiters to theSearchString
set theTextItems to every text item of theText
set AppleScript's text item delimiters to theReplacementString
set theText to theTextItems as string
set AppleScript's text item delimiters to ""
return theText
end findAndReplaceInText
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
on insertItemInList(theItem, theList, thePosition)
set theListCount to length of theList
if thePosition is 0 then
return false
else if thePosition is less than 0 then
if (thePosition * -1) is greater than theListCount + 1 then return false
else
if thePosition is greater than theListCount + 1 then return false
end if
if thePosition is less than 0 then
if (thePosition * -1) is theListCount + 1 then
set beginning of theList to theItem
else
set theList to reverse of theList
set thePosition to (thePosition * -1)
if thePosition is 1 then
set beginning of theList to theItem
else if thePosition is (theListCount + 1) then
set end of theList to theItem
else
set theList to (items 1 thru (thePosition - 1) of theList) & theItem & (items thePosition thru -1 of theList)
end if
set theList to reverse of theList
end if
else
if thePosition is 1 then
set beginning of theList to theItem
else if thePosition is (theListCount + 1) then
set end of theList to theItem
else
set theList to (items 1 thru (thePosition - 1) of theList) & theItem & (items thePosition thru -1 of theList)
end if
end if
return theList
end insertItemInList
on sortList(theList)
set the indexList to {}
set the sortedList to {}
set theListNames to {}
set sortedListNames to {}
-- Create a list with the names of the files
repeat with i from 1 to (count of theList)
set eachItem to item i of theList
tell application "Finder"
set end of theListNames to (name of eachItem as string)
end tell
end repeat
-- Sort the list of filenames alphabetically (by output number)
repeat (the number of items in theListNames) times
set the lowItem to ""
repeat with i from 1 to (number of items in theListNames)
if i is not in the indexList then
set thisItem to item i of theListNames
if the lowItem is "" then
set the lowItem to thisItem
set the lowItemIndex to i
else if thisItem comes before the lowItem then
set the lowItem to thisItem
set the lowItemIndex to i
end if
end if
end repeat
set the end of sortedListNames to the lowItem
set the end of the indexList to the lowItemIndex
end repeat
-- Use the index list to create a sorted list of the files themselves
repeat with eachItem in indexList
set end of sortedList to item eachItem of theList
end repeat
return the sortedList
end sortList
on listPosition(theItem, theList)
repeat with i from 1 to the count of theList
if item i of theList is theItem then return i
end repeat
return 0
end listPosition
on getFileName(theFile)
tell application "Finder"
set fileName to name of theFile
end tell
end getFileName