forked from DonovanChan/fmfunctions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppleScriptDirPath.calc
105 lines (101 loc) · 3.7 KB
/
AppleScriptDirPath.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
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" ) ;
message = If ( IsEmpty ( message ) ; "Please select a folder." ; message ) ;
defaultDirPath = GetValue ( defaultDirPath ; 1 ) ;
showInvisibles = (showInvisibles = True) ;
multipleSelection = (multipleSelection = True)
];
"set message to \"" & message & "\"¶
set showInvisibles to " & showInvisibles & " as boolean¶
set multipleSelection to " & multipleSelection & " as boolean¶
set resultError to null¶
set myFolder to {}¶
-- Prompt for folder¶
try¶
set myFolder to myFolder & choose folder with prompt message ¦" &
If ( not IsEmpty ( defaultDirPath ) ;
"default location POSIX file stripStartupDisk(\"" & defaultDirPath & "\") ¦"
) &
"invisibles showInvisibles ¦
multiple selections allowed multipleSelection¶
tell application \"Finder\"¶
set theItems to {}¶
repeat with dir in myFolder¶
set theItems to theItems & (dir as text)¶
end repeat¶
set dirPaths to my fileListToText(theItems,true)¶
end tell¶
on error errMsg number errNum¶
set resultError to \"Error \" & errNum & \": \" & errMsg¶
end try¶
¶
-- Determine result¶
if resultError is not null then¶
set resultFinal to resultError¶
else¶
set resultFinal to dirPaths¶
end if¶
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: 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¶
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: AppleScriptDirectoryPath ( resultFieldName ; message ; defaultDirPath ; showInvisibles ; multipleSelection )
PURPOSE: Prompts user for directory and returns paths of each file in first level of directory (as return-delimited list)
EXAMPLES:
HISTORY:
Created: 2012-06-12 PST -21 17h49 PST — Donovan A. Chandler
*/