Here’s a simple little script I wrote to save me the trouble maneuvering around in Filemaker when I want to do a find.
set theText to text returned of (display dialog "Organization name to search" default answer "")
tell application "FileMaker Pro"
tell database "Invoices.VMX"
go to layout "Cycling Order Form Dates"
delete every request
set myRequest to create request of it
set cell "Customer Full Address" of myRequest to theText
find
do script FileMaker script "AppleScript: Sort and go to last"
end tell
end tell
Is there a way to make that dialog box hold a popup list of recently entered text? Like 10 most recent searches?
The easiest way would be to add a return delimited global FM field that holds your
searches. When you run your script, retrieve the searches from that field as list
and and use them in a Choose from list dialog.
If it is a new search you would set the global field in FM with the 9 most recent
plus the new item.
Another way would be to read/write to a file but since you are already talking to
FM why not use it.
Regards,
Craig
If I were to use a text file, what would be the syntax to make the list drop down in the text field of the dialog box?
You cannot use a drop down in a display dialog. You will need to use a “choose from list” dialog
instead.
On your original display dialog add a button that says “Previous Search.” When the user clicks
this button give them the “choose from list” dialog with the previous searches.
hth,
Craig
Okay, first attempt at the bare bones of it.
How do I handle the “X most recent” functionality? Should I just write the text to the file in chronological order and when the file has more than X lines, delete line 1 and write new data at eof?
Also, I want the data to show up in the “choose from list” dialog in alphabetical order. Can I sort it on the fly?
set myData to {"one item", "another item", "some item"}
--set myFile to "myData.txt"
set myFile to ((path to documents folder as text) & "recentSearches.txt") as file specification
set fileref to open for access myFile with write permission
try
--set eof of fileref to 0 -- this erases it, don't do it to append
write myData to fileref
close access fileref
on error err -- this is to assure that you always close the file.
close access fileref
display dialog err
end try
set readData to (read myFile as list)
set theChoice to (choose from list readData)
display dialog theChoice as text
I would suggest making your text file a return separated list and not an actual list.
When you do that try this for the sort.
set myFile to ((path to documents folder as text) & "recentSearches.txt") as file specification
-- use this get and sort the last 10 in the file
set dataToChoose to paragraphs -1 thru -10 of (do shell script "/usr/bin/ruby -e 'print $stdin.readlines.sort' < " & quoted form of POSIX path of myFile)
When a new search is created write that choice to the end of recentSearches.txt
write (return & theChoice) to fileref starting at eof
Regards,
Craig
Cool! Almost there!
Only 2 mysteries remain:
-
The shell script doesn’t sort the items. It worked with a list but not with return-delimited text. Do I need to coerce the text to a list before passing it to the shell script?
-
Strange behavior: the text being returned from the ‘recent searches’ list looks normal, but there seem to be invisible characters in it which prevent the Filemaker search from finding anything. I put a ‘display dialog’ in there to show what’s happening. How can I get rid of it?
set myFile to ((path to documents folder as text) & "recentSearches.txt") as file specification
set readData to ""
try
close access myFile -- make sure it's closed
end try
tell application "Finder"
set recentSearchesExist to (exists myFile)
end tell
set fileref to open for access myFile with write permission
try
set readData to (read myFile)
end try
set searchCount to (count paragraphs of readData)
set myButtons to {"Cancel", "OK"}
if recentSearchesExist = true and readData ≠"" then
set beginning of myButtons to "Recent Searches"
end if
set theDialog to (display dialog "Organization name to search" default answer "" buttons myButtons default button "OK")
set {theText, theButton} to {text returned of theDialog, button returned of theDialog}
if theText ≠"" then
write (theText & return) to fileref starting at eof
end if
if searchCount > 20 then set searchCount to 20
if theButton = "Recent Searches" then
-- use this get and sort the last 20 in the file
set dataToChoose to paragraphs -1 thru -(searchCount - 1) of (do shell script "/usr/bin/ruby -e 'print $stdin.readlines.sort' < " & quoted form of POSIX path of myFile)
set theText to (choose from list dataToChoose)
end if
display dialog (characters of (theText as text)) -- note the weirdness when using items from the recent search list!
tell application "FileMaker Pro"
tell database "Invoices.VMX"
go to layout "Cycling Order Form Dates"
delete every request
set myRequest to create request of it
set cell "Customer Full Address" of myRequest to theText
find
do script FileMaker script "AppleScript: Sort and go to last"
end tell
end tell
try
--set eof of fileref to 0 -- this erases it, don't do it to append
--write myData to fileref
close access fileref
on error err -- this is to assure that you always close the file.
close access fileref
display dialog err
end try
You might try this.
-- I put the return fist so there is no trailing *blank* line in the file
write (return & theText) to fileref as «class utf8» starting at eof
If you are seeing extra spaces between the letters it’s because it is UTF-16 and you need UTF-8
The above should fix that.
I’ll check on the sort and get back with you.