forked from DonovanChan/fmfunctions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppleScriptFilePaths.calc
92 lines (88 loc) · 3.28 KB
/
AppleScriptFilePaths.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
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 =
Case (
PatternCount ( Get ( ApplicationVersion ) ; "ProAdvanced" ); "FileMaker Pro Advanced";
"FileMaker Pro"
) ;
message = If ( IsEmpty ( message ) ; "Please select a folder." ; message ) ;
defaultDirPath = GetValue ( defaultDirPath ; 1 )
];
"set fmpVersion to \"" & fmpVersion & "\"¶
set showInvisibles to " & GetAsBoolean ( showInvisibles ) & " as boolean¶
set multipleSelection to " & GetAsBoolean ( multipleSelection ) & " as boolean¶
set selectedFiles to {}¶
try¶
tell application \"Finder\"¶
set selectedFiles to selectedFiles & choose file with prompt \"" & message & "\" ¦" &
If ( not IsEmpty ( defaultDirPath ) ;
"default location POSIX file stripStartupDisk(\"" & defaultDirPath & "\") ¦"
) &
"invisibles showInvisibles¦
multiple selections allowed multipleSelection¶
end tell¶
set resultFinal to my fileListToText(selectedFiles,true)¶
on error errMsg number errNum¶
set resultFinal to \"Error \" & errNum & \": \" & errMsg¶
end try¶
setField(\"" & dbName & "\", \"" & tableName & "\", \"" & fieldName & "\", resultFinal)¶
¶
-- 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 \"Finder\"¶
set hdName to get name of startup disk¶
end tell¶
repeat with i in theList¶
set myPath to POSIX path of (i as alias)¶
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: Sets FileMaker field value¶
on setField(databaseName, tableName, fieldName, theValue)¶
tell application " & Quote ( 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: 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¶
¶
-- Handler: Returns path as text with startup disk removed¶
to stripStartupDisk(thePath)¶
set pathText to thePath as text¶
tell application \"Finder\"¶
set hdName to name of startup disk¶
end tell¶
set hdLen to length of hdName¶
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"
)
/* __________________________________________________
NAME: AppleScriptFilePaths ( resultFieldName ; message ; defaultDirPath ; showInvisibles ; multipleSelection )
PURPOSE: Prompts user for file(s) and returns paths of each file in first level of directory (as return-delimited list)
EXAMPLES:
HISTORY:
Created: 2011-06-21 17:49 PST - Donovan A. Chandler
Modified: 2012-04-24 14:02 PST - Donovan Chandler : dialog explicitly handled by finder (prevents beach ball in FileMaker)
*/