forked from DonovanChan/fmfunctions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppleScriptReadFile.calc
202 lines (197 loc) · 7.11 KB
/
AppleScriptReadFile.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
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
Let ( [
path = Get ( FilePath ) ; // parse name from path in case name has "." in it
dbName = Middle ( path ; Position ( path ; "/" ; Length ( path ) ; -1 ) + 1 ; 999 ) ;
fieldSplit = Substitute ( resultFieldName ; "::" ; ¶ ) ;
tableName = GetValue ( fieldSplit ; 1 ) ;
fieldName = GetValue ( fieldSplit ; 2 ) ;
message = If ( IsEmpty ( message ) ; "Select file you'd like to get the contents of." ; message ) ;
defaultDirPath = GetValue ( defaultDirPath ; 1 ) ;
fmpVersion =
Case (
PatternCount ( Get ( ApplicationVersion ) ; "ProAdvanced" ); "FileMaker Pro Advanced";
"FileMaker Pro"
)
] ;
"-- Localize parameters¶
set dbName to \"" & dbName & "\"¶
set fieldName to \"" & fieldName & "\"¶
set pathToFile to \"" & pathToFile & "\"¶
set promptMessage to \"" & message & "\"¶
set tableName to \"" & tableName & "\"¶
¶
-- Instantiate variables¶
set fileAlias to missing value¶
set fileText to missing value¶
set errMessage to missing value¶
¶
if length of pathToFile ≠ 0 then¶
try¶
set fileAlias to POSIX file stripStartupDisk(pathToFile) as alias¶
end try¶
end if¶
¶
try¶
-- Prompt for file if necessary¶
if fileAlias is missing value then¶
set fileAlias to choose file with prompt promptMessage" &
If ( not IsEmpty ( defaultDirPath ) ;
"¦default location POSIX file stripStartupDisk(\"" & defaultDirPath & "\") as alias"
) &
If ( not IsEmpty ( fileTypes ) ;
"¬¶of type utiList(\"" & fileTypes & "\")¶"
) &
" ¶
end if¶
¶
-- Read file¶
set fileText to readFile(fileAlias)¶
on error errText number errNumber¶
set errMessage to \"Error: \" & errNumber & \": \" & errText¶
end try¶
¶
-- Set to FileMaker record¶
if errMessage ≠ missing value¶
set resultText to errMessage¶
else¶
set resultText to fileText¶
set filePath to completePath(POSIX path of fileAlias)¶
set fileName to stripPath(fileAlias as text)¶
setField(dbName, tableName, fieldName, 2, filePath)¶
setField(dbName, tableName, fieldName, 3, fileName)¶
end¶
setField(dbName, tableName, fieldName, 1, resultText)¶
¶
-- Clear variable to ensure memory is clean¶
set fileText to missing value¶
¶
------------------------------------------------¶
-- HANDLERS¶
------------------------------------------------¶
¶
-- HANDLER: Prepends drive name to path¶
on completePath(pathFromUsers)¶
tell application \"Finder\"¶
set hdName to name of startup disk¶
end tell¶
hdName & pathFromUsers¶
end completePath¶
¶
-- HANDLER: Returns offset of last occurrence of character¶
on lastOffset(theString, theCharacter)¶
set strRev to (reverse of characters of theString) as text¶
set s to (length of theString) - (offset of theCharacter in strRev) + 1¶
end lastOffset¶
¶
-- HANDLER: Returns text from file. Prompts for file if no alias specified.¶
on readFile(fileAlias)¶
if fileAlias = \"\" then¶
set theFile to choose file with prompt (localized string \"chooseFile\")¶
else¶
set theFile to fileAlias¶
end if¶
try¶
tell application \"System Events\"¶
open for access theFile¶
set fileText to (read theFile)¶
close access theFile¶
return fileText¶
end tell¶
on error errMsg number errNum¶
try¶
close access theFile¶
end try¶
display dialog errMsg & return & errNum¶
end try¶
end readFile¶
¶
-- HANDLER: Searches and replaces string within text block¶
-- Accepts lists in searchString and replaceString¶
to searchReplaceText(theText, searchString, replaceString)¶
set searchString to searchString as list¶
set replaceString to replaceString as list¶
set theText to theText as text¶
¶
set oldTID to AppleScript's text item delimiters¶
repeat with i from 1 to count searchString¶
set AppleScript's text item delimiters to searchString's item i¶
set theText to theText's text items¶
set AppleScript's text item delimiters to replaceString's item i¶
set theText to theText as text¶
end repeat¶
set AppleScript's text item delimiters to oldTID¶
¶
return theText¶
end searchReplaceText¶
¶
-- HANDLER: Sets FileMaker field value¶
on setField(databaseName, tableName, fieldName, fieldRep, theValue)¶
tell application \"" & fmpVersion & "\"¶
tell database (databaseName as text)¶
tell table (tableName as text)¶
set repetition fieldrep of cell fieldName to theValue¶
end tell¶
end tell¶
end tell¶
end setField¶
¶
-- HANDLER: Strips directories from file path, leaving name only¶
-- Note: requires lastOffset() handler¶
on stripPath(thePath)¶
set nameStart to (my lastOffset(thePath, \":\")) + 1¶
return text nameStart thru (length of thePath) of thePath¶
end stripPath¶
¶
-- 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: Converts return-delimited list of labels into list of UTI's¶
-- Required Handlers: searchReplaceText¶
-- Use 'info for' command to see type idenfier of a file¶
-- More info: http://developer.apple.com/library/mac/#documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html#//apple_ref/doc/uid/TP40000983-CH216-SW4¶
on utiList(labelText)¶
set labels to {\"txt\",\"xml\",\"html\",\"log\",\"webarchive\",\"email archive\",\"css\"}¶
set values to {\"public.plain-text\",\"public.xml\",\"public.html\",\"com.apple.log\",\"com.apple.webarchive\",\"com.apple.mail.email\",\"com.apple.dashcode.css\"}¶
set newText to searchReplaceText(labelText,labels,values)¶
set myResult to paragraphs of newText¶
end¶
"
)
/* —————————————————————————————— //
NAME: AppleScriptReadFile ( resultFieldName ; message ; defaultDirPath ; pathToFile ; fileTypes )
PURPOSE:
Returns text contents and name of file.
Script will prompt for file if 'pathToFile' parameter is empty.
Returns two results - one to each repetition of the supplied result field.
rep 1: text from file
rep 2: path to file
rep 3: name of file
NOTES:
resultFieldName: Name of field AppleScript will return its results to
For best results, use a global field.
Field must be on layout when AS is performed.
defaultDirPath: Path to default folder.
Any of following formats are fine:
"filemac:HD/Users/donovan/Desktop/test.txt"
"/HD/Users/donovan/Desktop/test.txt"
"/Users/donovan/Desktop/test.txt"
pathToFile: (Optional) Path to file to read.
fileTypes: (Optional) List of file types user is permitted to select
Current options: css, email archive, html, log, txt, webarchive, xml
You can add new types by adding new type identifiers to utiList() handler
HISTORY:
Created: 2011-10-27 11:38 PST - Donovan Chandler
Modified: 2012-04-17 21:11 PST - Donovan Chandler
*/