forked from DonovanChan/fmfunctions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppleScriptChooseColor.calc
117 lines (111 loc) · 3.94 KB
/
AppleScriptChooseColor.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
Let ( [
path = Get ( FilePath ) ;
isFaulty = IsEmpty ( defaultColor ) or Left ( Trim ( defaultColor ) ; 3 ) ≠ "RGB" ;
defaultColor = If ( isFaulty ; "RGB(0;0;0)" ; defaultColor ) ;
dbName = Middle ( path ; Position ( path ; "/" ; Length ( path ) ; -1 ) + 1 ; 999 ) ;
fieldList = Substitute ( fullFieldName ; "::" ; ¶ ) ;
table = GetValue ( fieldList ; 1 ) ;
field = GetValue ( fieldList ; 2 ) ;
fmpVersion = "FileMaker Pro" & If ( Position ( Get ( ApplicationVersion ) ; "ProAdvanced" ; 1 ; 1 ) ; " Advanced" )
] ;
"set fmpVersion to \"" & fmpVersion & "\"¶
set fmResult to \"\"¶
set defaultColorRaw to \"" & defaultColor & "\"¶
set defaultColorArray to split(defaultColorRaw,\";\")¶
set defaultColor to adjustColorArray(defaultColorArray,false)¶
try¶
-- Ensure default color is valid¶
try¶
set defaultColor to adjustColorArray(defaultColorArray, false)¶
on error errText number errNum¶
--set fmResult to \"Error \" & errNum & \": \" & errText & \" Default color invalid: \\\"\" & defaultColorRaw & \"\\\"\"¶
set defaultColor to \"\"¶
end try¶
¶
-- Display dialog, removing default color if necessary¶
try¶
if defaultColor is \"\" then¶
set colorArray to choose color¶
else¶
set colorArray to choose color default color defaultColor¶
end if¶
on error errText number errNum¶
set fmResult to \"Error \" & errNum & \": \" & errText & \" Default color: \" & defaultColorRaw¶
end try¶
end try¶
-- Parse results if valid¶
if fmResult is \"\" then¶
set colorArrayNew to adjustColorArray(colorArray,true)¶
set fmResult to \"RGB (\" & item 1 of colorArrayNew & \";\" & item 2 of colorArrayNew & \";\" & item 3 of colorArrayNew & \")\"¶
end if¶
-- Send to FileMaker¶
setField(\"" & dbName & "\",\"" & table & "\",\"" & field & "\",fmResult)¶
¶
------------------------------------------------¶
-- HANDLERS¶
------------------------------------------------¶
¶
-- Handler: Converts RGB values to/from max of 65535 and 255¶
-- Required handlers: stripToInteger()¶
on adjustColorArray(array, to255)¶
set factor to 257¶
set newArray to {}¶
repeat with i in array¶
set num to my stripToInteger(i)¶
if to255 is true then¶
set end of newArray to ((num as integer) / factor) as integer¶
else¶
set end of newArray to ((num as integer) * factor) as integer¶
end if¶
end repeat¶
return newArray¶
end adjustColorArray¶
¶
--Handler: Splits string into array by delimiter¶
to split(someText, delimiter)¶
set AppleScript's text item delimiters to delimiter¶
set someText to someText's text items¶
set AppleScript's text item delimiters to {\"\"}¶
return someText¶
end split¶
¶
-- Handler: Removes everything but integers from string¶
on stripToInteger(someText)¶
set myResult to {}¶
set textItems to text items of (someText as string)¶
repeat with i in textItems¶
try¶
set end of myResult to (i as integer)¶
end try¶
end repeat¶
myResult as string¶
end stripToInteger¶
¶
---- FileMaker Handlers ----¶
------------------------------------------------¶
¶
-- 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"
)
/* —————————————————————————————— //
NAME:
AppleScriptChooseColor ( fullFieldName ; defaultColor )
PURPOSE:
Shows color picker and sets RGB value to FileMaker field
EXAMPLES:
AppleScriptChooseColor ( "FOCUS::TEMP_TEXT_GT" ; "RGB(255;0;0)" )
Presents color picker defaulted to red and returns "RGB(255;255;255)" in FOCUS::TEMP_TEXT_GT
HISTORY:
Created: 2010-Oct-14 10h26 PST — Donovan A. Chandler
Modified: 2012-Mar-19 10h05 PST — Donovan Chandler : enhanced error message on parameter conversion
NOTES:
defaultColor parameter currently not supported
*/