A script to find a file with a unique name

I am using the applescript function in filemaker pro.
I understand how to set a variable to the particular field containing my unique document name

set dog to cell "Document Name" of current record


But what I want to do is have either spotlight or finder show me that file in their “results” dialog: my ultimate aim is to open the document from that dialog.
Can someone help me with the syntax to accomplish this?
Thanks.

Using Shane STANLEY’s BridgePlus you may try :

use scripting additions
use framework "Foundation"
use script "BridgePlus"
load framework
set nameToSearch to "read the Dock.scpt"
set theResult to current application's SMSForder's runSpotlightQuery:"kMDItemFSName CONTAINS %@" queryValues:{nameToSearch} inFolders:{(path to desktop)} |error|:(missing value)
--ASify from theResult
-->	{"/Users/userName/Desktop/read the Dock.scpt"}
set posixFilesFound to theResult as list
-->	{"/Users/userName/Desktop/read the Dock.scpt"}
if posixFilesFound is {} then
	error "Don't find a file named "" & nameToSearch & ""."
end if
set theFile to POSIX file (posixFilesFound's item 1)
tell application "Finder"
	open theFile
end tell

Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) mardi 29 mars 2016 23:36:54

Are you able to show me where my variable “dog” fits in this script?
Wen I run it in applescript whatever I set “nameToSearch” to (including leaving it as ““read the Dock.scpt”) I get the following error message:
error “Can’t get item 1 of {}.” number -1728 from item 1 of {}”

If you have AppleScript Toolbox installed:

set dog to cell "Document Name" of current record
set theFiles to AST query metadata "kMDItemFSName = '*" & dog & "*'"
if (count of theFiles) > 0 then
	tell application "Finder" to open (first item of theFiles)
end if

remove the surrounding asterix, they are wildcards and makes the comparison. You can extend the command by appending ‘only in folders’ so it only looks for files located on your desktop like Yvan’s example.

When I try to run this in apple script it stops on “Finder” and tells me it expected the end of the line but found an identifier.

When I try to run it in filemaker it sticks on “query” and says Expected end of line, etc. but found identifier.

You probably don’t have AppleScript toolbox.osax installed, like I mentoned, it is required. I just copied the set dog to. line from your code because I don’t have Filemaker so I assumed the variable dog would contain a string (without quotes).

I Installed the Applescript toolbox before I tried your suggestion in applescript proper - as I say when running it in applescript proper it only stops when it gets to the word “Finder”.
I am thinking that filemaker’s ability to run the toolbox may be the issue so I am just trying to get it to work outside filemaker and when I do that I will see what happens inside and then, if I need to, see if I can call a working script using the toolbox from within filemaker, if that makes sense.

I assumed that the name : nameToSearch given to the variable containing the file name to search was clear enough.
If you prefer to name it dog, it’s your problem.
I’m not really surprised to read that the script returns an empty list on your machine because “read the Dock.scpt” is the name of one of my scripts.
I added a test in my original message to get rid of that.

Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) mercredi 30 mars 2016 08:45:29

The “expected end of” error is commonly an compilation error which normally indicates that the code is wrong or the dictionary is not loaded. I think you’re right that the osax is not properly loaded by filemaker and I will look into this today. Thanks for the reply.

Thanks for your efforts - it worked.
forgive my ignorance.
It wont work from within filemaker, however. The error message says

It is in a filemaker dialog box

I apologize but I don’t know Filemaker specs.

Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) mercredi 30 mars 2016 12:16:25

Here is a script that works well from the Script Editor.
I don’t know if it will run in a FileMaker Script block or not.
You may need to make adjustments to make it run there.

It does NOT require any external scripting additions or script libraries.

This should be very fast, especially if you can give it the top-level folder to start searching from.
It will search that folder, and all sub-folders.

EDIT: 2016-03-31 20:46 CT – Ver 1.1
¢ Add check for no file matches
¢ Add comments for using blanks in path



(*
===============================================================================
	[FM] Open File from FileMaker Cell
===============================================================================

VER: 	1.1		LAST UPDATE:   2016-03-31

PURPOSE:
	¢ Open the file whose name is in the FileMaker cell "Document Name"

AUTHOR:		JMichaelTX

REQUIRED:
	1.	Mac OS X Yosemite 10.10.5+
	2.	Mac Applications
		¢ FileMaker
	
REF:  The following were used in some way in the writing of this script.
	1.	A script to find a file with a unique name
		[url=http://macscripter.net/viewtopic.php?id=44776]IMPORTANT NOTE:
	¢ This script has NOT been tested in FileMaker.
	¢ It was tested in the Script Editor
	¢ It may or may not work in a FileMaker script block
===============================================================================
   
*)
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

--- "Set dog to cell..." NOT TESTED Since I don't have FileMaker ---
--		Uncomment for production use in File Maker
--set dog to cell "Document Name" of current record

--- USE FOR TESTING ---
--		Set to the file name (all or partial) to search for
--		Comment out (or delete) for production use in FileMaker
set dog to "YourFileName" ### SET ACCORDING TO YOUR NEEDS ###

-- Not sure if this is needed since I don't know the class of 
--   a File Maker cell.  But a text value is needed.
set dogStr to dog as text

---------------------------------------------------------------
--- CREATE COMMAND LINE FOR mdfind (Spotlight command line) ---
---------------------------------------------------------------

-- IF you know which folder (or sub-folders thereof)
-- that the File Maker file will be using, then set it here
-- For your HOME folder, set to "~"
-- It will search/find in that folder, and all sub-folders
-- If path contains blanks, they must be escaped using double backslashes, as in:
--    "~/Document/My\\ Folder\\ Name"

set folderToSearch to "~/Documents" ### SET ACCORDING TO YOUR NEEDS ###

-- Speed up search/find by setting the file "kind"

set kindStr to ""

--- BUILD mdfind COMMAND STRING ---
set cmdStr to "mdfind -onlyin " & folderToSearch & " name:" & dogStr
if kindStr ≠ "" then set cmdStr to cmdStr & " kind:" & kindStr

--- RUN the mdfind COMMAND ---
set findResults to do shell script cmdStr

---------------------------------------------------------------
--- PROCESS RESULTS of mdfind ---
---------------------------------------------------------------
--		Results are returned a text lines
--		convert text to List
set fileList to paragraphs of findResults

set dogFilePath to ""

---------------------------------------------------------------
--- IF MORE THAN ONE FILE FOUND, ASK USER TO CHOOSE ---
---------------------------------------------------------------

set numFiles to count of fileList

if numFiles > 1 then -- SHOW CHOOSER
	
	set oAns to choose from list fileList ¬
		with title ¬
		"CHOOSE FILE" with prompt "Multiple Files were found that match: " & dogStr ¬
		& return & ¬
		"Choose ONE to Open" default items {item 1 of fileList} ¬
		OK button name ¬
		"Open" cancel button name ¬
		"Cancel" multiple selections allowed false ¬
		without empty selection allowed
	
	if oAns is not false then
		set dogFilePath to item 1 of oAns
	end if
	
else if numFiles = 1 then -- ONLY ONE FILE FOUND
	set dogFilePath to item 1 of fileList
else -- NO FILES WERE FOUND
	
	beep
	display dialog "NO files were found that match: " & dogStr ¬
		with title ¬
		"NO FILES FOUND" with icon stop ¬
		
end if

---------------------------------------------------------------
--- OPEN THE FILE IN ITS DEFAULT APP ---
---------------------------------------------------------------

if dogFilePath ≠ "" then
	tell application "Finder"
		open POSIX file dogFilePath as alias
	end tell
end if

--~~~~~~~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~~~~~~~~~~~~~~~

Thank you. Thank you. Thank you.
That works like a charm.
I have posted the one I have actually implemented in filemaker which might give an amateur some clues to how they implement your scrip in filemaker.
Thanks to all who contributed.

Here is the script as implemented in filemaker:



(*
===============================================================================
	[FM] Open File from FileMaker Cell
===============================================================================

VER: 	1.1		LAST UPDATE:   2016-03-31

PURPOSE:
	¢ Open the file whose name is in the FileMaker cell "Document Name"

AUTHOR:		JMichaelTX

REQUIRED:
	1.	Mac OS X Yosemite 10.10.5+
	2.	Mac Applications
		¢ FileMaker
	
REF:  The following were used in some way in the writing of this script.
	1.	A script to find a file with a unique name
		[url=http://macscripter.net/viewtopic.php?id=44776]IMPORTANT NOTE:
	¢ This script has NOT been tested in FileMaker.
	¢ It was tested in the Script Editor
	¢ It may or may not work in a FileMaker script block
===============================================================================
   
*)
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

--- "Set dog to cell "Document Name" of current record..." NOT TESTED Since I don't have FileMaker ---
--		Uncomment for production use in File Maker
--set dog to cell "Document Name" of current record

--- USE FOR TESTING ---
--		Set to the file name (all or partial) to search for
--		Comment out (or delete) for production use in FileMaker
Set dog to cell "Document Name" of current record
-- Not sure if this is needed since I don't know the class of 
--   a File Maker cell.  But a text value is needed.
set dogStr to dog as text

---------------------------------------------------------------
--- CREATE COMMAND LINE FOR mdfind (Spotlight command line) ---
---------------------------------------------------------------

-- IF you know which folder (or sub-folders thereof)
-- that the File Maker file will be using, then set it here
-- For your HOME folder, set to "~"
-- It will search/find in that folder, and all sub-folders
-- If path contains blanks, they must be escaped using double backslashes, as in:
--    "~/Document/My\\ Folder\\ Name"

set folderToSearch to "~" ### SET ACCORDING TO YOUR NEEDS ###

-- Speed up search/find by setting the file "kind"

set kindStr to ""

--- BUILD mdfind COMMAND STRING ---
set cmdStr to "mdfind -onlyin " & folderToSearch & " name:" & dogStr
if kindStr ≠ "" then set cmdStr to cmdStr & " kind:" & kindStr

--- RUN the mdfind COMMAND ---
set findResults to do shell script cmdStr

---------------------------------------------------------------
--- PROCESS RESULTS of mdfind ---
---------------------------------------------------------------
--		Results are returned a text lines
--		convert text to List
set fileList to paragraphs of findResults

set dogFilePath to ""

---------------------------------------------------------------
--- IF MORE THAN ONE FILE FOUND, ASK USER TO CHOOSE ---
---------------------------------------------------------------

set numFiles to count of fileList

if numFiles > 1 then -- SHOW CHOOSER
	
	set oAns to choose from list fileList ¬
		with title ¬
		"CHOOSE FILE" with prompt "Multiple Files were found that match: " & dogStr ¬
		& return & ¬
		"Choose ONE to Open" default items {item 1 of fileList} ¬
		OK button name ¬
		"Open" cancel button name ¬
		"Cancel" multiple selections allowed false ¬
		without empty selection allowed
	
	if oAns is not false then
		set dogFilePath to item 1 of oAns
	end if
	
else if numFiles = 1 then -- ONLY ONE FILE FOUND
	set dogFilePath to item 1 of fileList
else -- NO FILES WERE FOUND
	
	beep
	display dialog "NO files were found that match: " & dogStr ¬
		with title ¬
		"NO FILES FOUND" with icon stop ¬
		
end if

---------------------------------------------------------------
--- OPEN THE FILE IN ITS DEFAULT APP ---
---------------------------------------------------------------

if dogFilePath ≠ "" then
	tell application "Finder"
		open POSIX file dogFilePath as alias
	end tell
end if

--~~~~~~~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~~~~~~~~~~~~~~~

I plan to clean it up and remove the “dog” variable :slight_smile:

It seems that an instruction need to be edited :


# yours
set cmdStr to "mdfind -onlyin " & folderToSearch & " name:" & dogStr
# Edited
set cmdStr to "mdfind -onlyin " & quoted form of folderToSearch & " name:" & dogStr

It would get rid of possible space or other “special” characters.

You wrote :

Set dog to cell "Document Name" of current record
-- Not sure if this is needed since I don't know the class of 
-- a File Maker cell. But a text value is needed.
set dogStr to dog as text

You may try to use - temporarily -

Set dog to cell "Document Name" of current record

display dialog "class of dog is : "& class of dog # ADDED
-- Not sure if this is needed since I don't know the class of 
-- a File Maker cell. But a text value is needed.
set dogStr to dog as text

In fact, the explicit coercion to string is not required.
Every instructions using dogStr are of the kind :

set cmdStr to "mdfind -onlyin " & folderToSearch & " name:" & dogStr

Concatenating dogStr to what is already a string coerce automatically dogStr into string.
Here,
"mdfind -onlyin " is a string, so
"mdfind -onlyin " & folderToSearch is a string, so
“mdfind -onlyin " & folderToSearch & " name:” is a string, so
set cmdStr to “mdfind -onlyin " & folderToSearch & " name:” & dog is a string, even if dog is not a string, a list for instance.

Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) vendredi 1 avril 2016 11:17:51

Yvan, thanks for your suggestions.

When I tested your statement with the quoted form it did NOT work.
mdfind did NOT find any files.

The cmdStr becomes:
“mdfind -onlyin ‘~/Documents/Test’ name:excel”

The command runs fine without the single quotes:
“mdfind -onlyin ~/Documents/Test name:excel”

I don’t think mdfind likes the single quotes, which are added by the quoted form of command.

You’re right, mdfind doesn’t apply on quoted path.
Consequence, it doesn’t apply if the passed path contain space or other “special” character.
Try to rename the Test folder as “Test 2”

Really annoying feature.

Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) samedi 2 avril 2016 11:48:18

Guys,

of course mdfind does consider quoted paths.

The issue is that a tilde (~) is not implicitly expanded when using quoted form of

This works

set folderToSearch to POSIX path of (path to documents folder) & "Test"
set dogStr to "excel"
set cmdStr to "mdfind -onlyin " & quoted form of folderToSearch & " name:" & dogStr
do shell script cmdStr

Hello Stefan

I re-made tests with :

set folderToSearch to POSIX path of ((path to documents folder as text) & "Test 2:")
set dogStr to "2014-06-17T21.27.45 sysPref Dock.png"
set cmdStr to "mdfind -onlyin " & quoted form of folderToSearch & " name:" & dogStr
do shell script cmdStr
log result (**)
set cmdStr to "mdfind -onlyin " & folderToSearch & " name:" & dogStr
do shell script cmdStr
log result (**)
tell application "System Events"
	exists file (folderToSearch & dogStr)
	log result (*true*)
end tell
set dogStr to "Excel"
set cmdStr to "mdfind -onlyin " & quoted form of folderToSearch & " name:" & dogStr
do shell script cmdStr
log result (*/Users/userName/Documents/Test/excel*)
set cmdStr to "mdfind -onlyin " & folderToSearch & " name:" & dogStr
do shell script cmdStr
log result (**)
tell application "System Events"
	exists file (folderToSearch & dogStr)
	log result (*true*)
end tell

set folderToSearch to POSIX path of ((path to documents folder as text) & "Test:")
set dogStr to "2014-06-17T21.27.45sysPrefDock.png"
set cmdStr to "mdfind -onlyin " & quoted form of folderToSearch & " name:" & dogStr
do shell script cmdStr
log result (**)
set cmdStr to "mdfind -onlyin " & folderToSearch & " name:" & dogStr
do shell script cmdStr
log result (**)
tell application "System Events"
	exists file (folderToSearch & dogStr)
	log result (*true*)
end tell
set dogStr to "Excel"
set cmdStr to "mdfind -onlyin " & quoted form of folderToSearch & " name:" & dogStr
do shell script cmdStr
log result (*/Users/userName/Documents/Test/excel*)
set cmdStr to "mdfind -onlyin " & folderToSearch & " name:" & dogStr
do shell script cmdStr
log result (*/Users/userName/Documents/Test/excel*)
tell application "System Events"
	exists file (folderToSearch & dogStr)
	log result (*true*)
end tell

It seems that this morning my eyes weren’t wide open.
When the folder path contain a space,
no instruction find the really existing file whose name is “2014-06-17T21.27.45 sysPref Dock.png”
the one using quoted form find the file named Excel
the one using the non-quoted path missed the file named Excel

When the folder path doesn’t contain a space,
no instruction find the really existing file whose name is “2014-06-17T21.27.45sysPrefDock.png”
Yes, I deliberately created a file whose spaces were removed in the filename.
the one using quoted form find the file named Excel
the one using the non-quoted path find the file named Excel

Why are the instructions unable to find the “long” filenames ?

Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) samedi 2 avril 2016 15:26:30

We are still in the shell environment, so ALL parameters containing space characters must be escaped or quoted

set cmdStr to "mdfind -onlyin " & quoted form of folderToSearch & space & quoted form of ("name:" & dogStr)