forked from DonovanChan/fmfunctions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppleScriptMoveFiles.calc
173 lines (167 loc) · 5.82 KB
/
AppleScriptMoveFiles.calc
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
Let ([
path = Get ( FilePath ) ;
dbName = Middle ( path ; Position ( path ; "/" ; Length ( path ) ; -1 ) + 1 ; 999 ) ;
fieldSplit = Substitute ( resultFieldName ; "::" ; ¶ ) ;
tableName = GetValue ( fieldSplit ; 1 ) ;
fieldName = GetValue ( fieldSplit ; 2 ) ;
fmpVersion = "FileMaker Pro" & If ( Position ( Get ( ApplicationVersion ) ; "ProAdvanced" ; 1 ; 1 ) ; " Advanced" )
];
"-- Localize parameters
set resultError to null¶
set filePaths to \"" & filePaths & "\"¶
set destinationPath to \"" & destinationPath & "\"¶
set createDestination to " & GetAsBoolean ( createDestination ) & " as boolean¶
set copySourceFiles to " & GetAsBoolean ( copySourceFiles ) & " as boolean¶
set overwrite to " & GetAsBoolean ( overwrite ) & " as boolean¶
set dbName to \"" & dbName & "\"¶
set fieldName to \"" & fieldName & "\"¶
set tableName to \"" & tableName & "\"¶
¶
-- Massage parameters¶
set filePaths to trimLinesRight(filePaths)¶
set destinationPathPOSIX to stripStartupDisk(trimLinesRight(destinationPath))¶
set destinationPath to (POSIX file destinationPathPOSIX) as text¶
set resultError to missing value¶
¶
-- Create or validate directory¶
if createDestination then¶
do shell script \"mkdir -p \" & quoted form of destinationPathPOSIX¶
else if not fileExists(destinationPathPOSIX) then¶
set resultFinal to \"Error: destinationPath not valid (\" & destinationPathPOSIX & \")\"¶
setField(dbName, tableName, fieldName, resultFinal)¶
return¶
end if¶
¶
set fileList to paragraphs of filePaths¶
set resultPathList to {}¶
¶
-- Move each file¶
repeat with theFileString in fileList¶
try¶
set theFile to POSIX file (stripStartupDisk(theFileString))¶
tell application \"Finder\"¶
if copySourceFiles then¶
duplicate file theFile to folder destinationPath replacing overwrite¶
set newFile to destinationPath & my fileName(theFile)¶
else¶
set newFile to move file theFile to folder destinationPath replacing overwrite¶
end if¶
end tell¶
set end of resultPathList to newFile¶
on error errMsg number errNum¶
-- Optionally delete file¶
if errNum is -15267 and copySourceFiles is false then¶
tell application \"Finder\" to delete theFile¶
else¶
set resultError to \"Error \" & errNum & \": \" & errMsg¶
end if¶
if errNum is -10000 then¶
set resultError to resultError & return & \"Invalid path: \" & theFileString¶
end if¶
end try¶
end repeat¶
¶
-- Determine result¶
if resultError is not missing value then¶
set resultFinal to resultError¶
else¶
set resultFinal to my fileListToText(resultPathList, true)¶
end if¶
¶
-- Send result to FileMaker¶
setField(dbName, tableName, fieldName, resultFinal)¶
¶
------------------------------------------------¶
-- HANDLERS¶
------------------------------------------------¶
¶
-- HANDLER: Returns true if file or folder exists¶
on fileExists(fileOrFolderPOSIXPath)¶
try¶
alias (POSIX file fileOrFolderPOSIXPath)¶
return true¶
on error¶
return false¶
end try¶
end fileExists¶
¶
-- HANDLER: Returns list of files as return-delimited text string¶
-- addStartupDrive allows prepending of disk name to each path¶
to fileListToText(theList, addStartupDrive)¶
set theText to \"\"¶
tell application \"System Events\"¶
set hdName to get name of startup disk¶
end tell¶
repeat with i in theList¶
set myPath to POSIX path of (i as alias)¶
--set myPath to POSIX path of i¶
if addStartupDrive is true then set myPath to hdName & (myPath as text)¶
set theText to theText & myPath & return¶
end repeat¶
return trimLinesRight(theText)¶
end fileListToText¶
¶
-- HANDLER: Returns name of file in path¶
-- Path can be AppleScript HFS (\":\") or POSIX (\"/\")¶
on fileName(pathOrFile)¶
set pathStr to pathOrFile as text¶
if (offset of \":\" in pathStr) > 0 then¶
set delimNew to \":\"¶
else¶
set delimNew to \"/\"¶
end if¶
set delimDefault to AppleScript's text item delimiters¶
set AppleScript's text item delimiters to delimNew¶
set fileName to text item -1 of pathStr¶
set AppleScript's text item delimiters to delimDefault¶
return fileName¶
end fileName¶
¶
-- HANDLER: Sets FileMaker field value¶
on setField(databaseName, tableName, fieldName, theValue)¶
tell application \"" & fmpVersion & "\"¶
tell database (databaseName as text)¶
tell table (tableName as text)¶
set field fieldName to theValue¶
end tell¶
end tell¶
end tell¶
end setField¶
¶
-- HANDLER: Returns path as text with startup disk removed¶
to stripStartupDisk(thePath)¶
set pathText to thePath as text¶
tell application \"System Events\"¶
set hdName to name of startup disk¶
end tell¶
set hdLen to length of hdName¶
set colonPos to offset of \":\" in pathText¶
if colonPos is greater than 0 then set pathText to text (colonPos + 1) thru -1 of pathText¶
if character 1 of pathText is equal to \"/\" then set pathText to text 2 thru -1 of pathText¶
if text 1 thru hdLen of pathText is equal to hdName then¶
set pathText to text (hdLen + 1) thru -1 of pathText¶
end if¶
return pathText¶
end stripStartupDisk¶
¶
-- HANDLER: Removes trailing newlines¶
to trimLinesRight(theText)¶
repeat while theText ends with return¶
set theText to theText's text 1 thru -2¶
end repeat¶
return theText¶
end trimLinesRight"
)
/* —————————————————————————————— //
NAME:
AppleScriptMoveFiles ( resultFieldName ; filePaths ; destinationPath ; createDestination ; copySourceFiles ; overwrite )
PURPOSE:
Moves files to new directory. Returns list of paths to new file locations.
EXAMPLES:
NOTES:
Will replace existing files in destination if overwrite = True.
Else, will delete file from original location unless copySourceFiles = True.
Files not moved/copied will not be listed in the result.
HISTORY:
Created: 2011-Jun-28 11h15 PST — Donovan A. Chandler
*/