-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.txt
304 lines (252 loc) · 9.58 KB
/
script.txt
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
on run {input, parameters}
#Static Variables
set noFilesPassed to "No files were passed as input."
set filesEmpty to "Can not create a password protected archive from empty files. Please try selecting files that have data stored."
set errorOccured to "An unexpected error has occured. Please try again."
set dialogTitle to "Create Archive"
set bundleInputString to "Archive"
set protectedString to "(Protected)"
set archiveName to bundleInputString & " " & protectedString
set zipIcon to {"/System/Library/CoreServices/Applications/Archive Utility.app/Contents/Resources/au-zip.icns", "/System/Library/CoreServices/Applications/Archive Utility.app/Contents/Resources/ArchiveUtility.icns", note}
set cautionIcon to {"/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertCautionIcon.icns", caution}
set stopIcon to {"/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertStopIcon.icns", stop}
set archiveExtension to "zip"
set archiveExcludeHiddenMacFiles to true
set hiddenMacFiles to {"*.DS_Store", "*__MACOSX"}
#Main Script
--Check Input
if (count of input) = 0 then
return noFilesPassed
end if
--Validate Icons
set zipIcon to resolveIcon(zipIcon)
set cautionIcon to resolveIcon(cautionIcon)
set stopIcon to resolveIcon(stopIcon)
--Validate Input Sizes
set inputFilesEmpty to validateInputEmpty(input)
if inputFilesEmpty = true then
tell me to activate
tell application "Finder"
display dialog filesEmpty buttons {"OK"} default button 1 with title dialogTitle with icon stopIcon
return
end tell
end if
--Get Correct File Name
set archiveDefaultName to true
if (count of input) = 1 then
set archiveDefaultName to false
--set currentFileName to name of (info for input's item 1) --includes extension
set currentFileName to removeExtension(name of (info for input's item 1)) --removed extension
set archiveName to currentFileName & " " & protectedString
end if
--Determine Output Path
set outputDirectory to getDirectory(input)
--Rename Archive If It Already Exists
set tempArchiveName to archiveName
set tempZipFile to tempArchiveName & "." & archiveExtension
set tempFullFile to outputDirectory & tempZipFile
if fileExists(tempFullFile) = true then
set archiveDefaultName to false
set incrementNumber to 2
repeat
set tempFullFile to outputDirectory & tempArchiveName & " " & incrementNumber & "." & archiveExtension
if fileExists(tempFullFile) = false then
exit repeat
end if
set incrementNumber to incrementNumber + 1
end repeat
set archiveName to archiveName & " " & incrementNumber
end if
--Set Correct Dialog Text
set passwordDisplayText to "Please enter a password for this archive."
set passwordDisplayTextSpecific to "Please enter a password for the archive “" & archiveName & "”."
set passwordVerifyText to "Please verify the password for this archive."
set passwordVerifyTextSpecific to "Please verify the password for the archive “" & archiveName & "”."
set passwordTooShortText to "The password can not be empty. Please try again by entering a longer password for this archive."
set passwordTooShortTextSpecific to "The password can not be empty. Please try again by entering a longer password for the archive “" & archiveName & "”."
set passwordMissMatchText to "The passwords were mismatched. Please try again by entering a password for this archive."
set passwordMissMatchTextSpecific to "The passwords were mismatched. Please try again by entering a password for the archive “" & archiveName & "”."
if archiveDefaultName = false then
set passwordDisplayText to passwordDisplayTextSpecific
set passwordVerifyText to passwordVerifyTextSpecific
set passwordTooShortText to passwordTooShortTextSpecific
set passwordMissMatchText to passwordMissMatchTextSpecific
end if
--Password Dialog
set passwordDisplayCaution to false
repeat
considering case
tell me to activate
tell application "Finder"
repeat
if passwordDisplayCaution = false then
set initPass to text returned of (display dialog passwordDisplayText default answer "" with title dialogTitle with icon zipIcon with hidden answer)
else
set initPass to text returned of (display dialog passwordDisplayText default answer "" with title dialogTitle with icon cautionIcon with hidden answer)
end if
if (length of initPass) > 0 then
exit repeat
else
set passwordDisplayCaution to true
set passwordDisplayText to passwordTooShortText
end if
end repeat
set finalPass to text returned of (display dialog passwordVerifyText buttons {"OK"} default button 1 default answer "" with title dialogTitle with icon zipIcon with hidden answer)
end tell
if finalPass = initPass then
exit repeat
else
set passwordDisplayCaution to true
set passwordDisplayText to passwordMissMatchText
end if
end considering
end repeat
--Determine Output File
set outputZipFile to "./\"" & archiveName & "." & archiveExtension & "\""
--Ignored Files
set ignoreFilesText to ""
if archiveExcludeHiddenMacFiles = true then
set ignoreFilesText to excludeFilesToString(hiddenMacFiles)
end if
--List of Files to Zip
set inputFiles to inputToFilePathsText(input, outputDirectory)
set directoryCommand to "cd \"" & outputDirectory & "\""
set zipCommand to "zip " & ignoreFilesText & " -P \"" & finalPass & "\" -qr " & outputZipFile & " " & inputFiles
set fullCommand to directoryCommand & "; " & zipCommand
--Execute the Commands
tell application "Finder"
try
do shell script fullCommand
on error
display dialog errorOccured buttons {"OK"} default button 1 with title dialogTitle with icon stopIcon
return
end try
end tell
return input
end run
#Script Functions
--Resolve Icons
on resolveIcon(iconsList)
set fileName to first item of iconsList
set restList to {}
if (count of items in iconsList) is greater than 1 then
set restList to (items 2 thru (count of items in iconsList) of iconsList)
end if
try
fileName as alias
on error
try
set fileName to fileName as POSIX file
fileName as alias
on error
if (count of items in restList) is greater than 0 then
set fileName to resolveIcon(restList)
end if
end try
end try
return fileName
end resolveIcon
--Validate File Existence
on fileExists(inputFile)
set doesFileExist to true
try
inputFile as alias
on error
try
set inputFile to inputFile as POSIX file
inputFile as alias
on error
set doesFileExist to false
end try
end try
return doesFileExist
end fileExists
--Validate Input Sizes
on validateInputEmpty(inputFiles)
repeat with inputFile in inputFiles
if (kind of (info for inputFile)) = "Folder" then
try
set tempFolderList to (list folder (inputFile as POSIX file) without invisibles)
on error
set tempFolderList to (list folder (inputFile) without invisibles)
end try
set fullFolderList to completePathsFolderItems(tempFolderList, inputFile)
set folderResult to validateInputEmpty(fullFolderList)
if folderResult = false then
return false
end if
else
if (size of (info for inputFile)) > 0 then
return false
end if
end if
end repeat
return true
end validateInputEmpty
--Remove Extensions
on removeExtension(fileName)
if fileName contains "." then
set fileName to (the reverse of every character of fileName) as string
set x to the offset of "." in fileName
set fileName to (text (x + 1) thru -1 of fileName)
set fileName to (the reverse of every character of fileName) as string
end if
return fileName
end removeExtension
--Get Directory
on getDirectory(inputFiles)
set firstItemAlias to inputFiles's item 1
set firstItemPath to POSIX path of firstItemAlias
set firstItemInfo to info for firstItemAlias
set firstItemName to name of firstItemInfo
tell application "Finder" to set parentDirectory to POSIX path of ((container of firstItemAlias) as text)
return parentDirectory
end getDirectory
--Exclude Files
on excludeFilesToString(inputExcludeStrings)
set outputExcludeString to ""
set incrementNumber to 0
repeat with inputString in inputExcludeStrings
if incrementNumber > 0 then
set outputExcludeString to outputExcludeString & " "
end if
set outputExcludeString to outputExcludeString & "-x " & "\"" & inputString & "\""
set incrementNumber to incrementNumber + 1
end repeat
return outputExcludeString
end excludeFilesToString
--List Files
on inputToFilePathsText(inputFiles, directory)
set outputFilesString to ""
set incrementNumber to 0
repeat with inputFile in inputFiles
if incrementNumber > 0 then
set outputFilesString to outputFilesString & " "
end if
set currentPath to POSIX path of inputFile
set currentPath to replaceText(currentPath, directory, "")
set outputFilesString to outputFilesString & "\"" & currentPath & "\""
set incrementNumber to incrementNumber + 1
end repeat
return outputFilesString
end inputToFilePathsText
--Add Prefix Path to Folder Lists
on completePathsFolderItems(inputList, prefixPath)
set tempList to {}
repeat with inputItem in inputList
copy ((prefixPath as text) & inputItem) as alias to the end of tempList
end repeat
return tempList
end completePathsFolderItems
--Replace Text
on replaceText(inputText, searchText, replaceWithText)
considering case
set storeASDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to searchText
set tempList to every text item of inputText
set AppleScript's text item delimiters to replaceWithText
set inputText to tempList as string
set AppleScript's text item delimiters to storeASDelimiters
return inputText
end considering
end replaceText