I need an Applescript that can be called from within a FMPro script to change the name of an external file. Specifically I plan to set up a FMPro script to export a set of records to a file, then change the file name before exported the next set of records to the original file name. I am still using FMPro 6.0 which allows me to specify the file name within the script, but does not let me change it without using a dialogue window. To complicate matters, I need to be able to identify the new file name from a field within the FMPro database.
I am a rank amateur with Applescript. Thanks for any help you can offer.
I’m not overly familiar with FM 6, but this should work…
For this example I have a file on my desktop called oldFileName.zip. Then I have a FileMaker database with two fields and only one record as shown below.
Record 1 cell: new file name value: newFileName.zip cell: old file name value: oldFileName.zip
Then within FileMaker I created a native AppleScript with the following contents.
set deskPath to path to desktop as string
set {oldName, newName} to {cell "old file name", cell "new file name"} of current record
tell application "Finder" to set name of alias (deskPath & oldName) to newName
I ran the script and the file renamed without a problem.
I created the FMPro database with one record and two fields as you indicated. Then I opened Scriptmaker in FMPro and created a script that had one step to perform an Applescript script. Inside it, I pasted your script. When I tried to run it, I got an error message that deskPath was not defined.
Interesting it shouldn’t need to be in a finder tell block, but I can’t test it in 6 just 7 & 8. From looking at some old 6 scripts though that looks like it should work
tell application "Finder"
set deskPath to path to desktop as string
set {oldName, newName} to {cell "old file name", cell "new file name"} of current record
tell application "Finder" to set name of alias (deskPath & oldName) to newName
end tell
Got error message…
Expected “,” or “}” but found ". (Error -2741) with the " before ‘old file name’.
With some help from the FMPro list, here is a script that appears to work.
Tell application "Finder"
set deskPath to (path to desktop folder from user domain) as string
end tell
tell application "Filemaker"
set {oldName, newName} to {cell "old file name", cell "new file name"} of current record
end tell
tell application "Finder" to set name of alias (deskPath & oldName) to newName
FileMaker’s use of AppleScript has always struck me as a tad odd. You definitely have to break things down and nest them differently. If I had a buck for every time a script worked fine in Script Editor but not when I pasted it into FileMaker…I’d have a much heavier wallet.
LOL! Thats the way I feel about FM and a few strange printing issues. I must be luck though on the scripting side as I’ve never ran into any real oddities that come to mind.
I suppose it’s possible that FMP 6 has an issue with the path to command (as does System Events, for example). Since I can’t test for it here, I couldn’t say for certain. However, I can confirm that the error number -2741, reported by Charlie above (message #6), is a syntax type error: “expected but found ”. When using a script editor, something like this would normally be identified as a compile time error. However, under other circumstances, such issues may not manifest themselves until the script is run.
The problem was almost certainly caused by wrapping the Finder tell statement around the entire code (including the FileMaker statement and another Finder tell). This would have effectively moved the statement intended for execution by Filemaker beyond FMP’s scope. And since the term cell is recognised by neither AppleScript nor Finder, it would then be assumed to be a user-defined variable.
In such circumstances, the compiler would believe it was dealing with a statement like the one below (which shouldn’t compile in a script editor):
set {variable_A, variable_B} to {variable_X "text X", variable_Y "text Y"}
”> compile error (number -2741): "Expected ", " or "}" but found "."
The error simply means that, because we’re dealing with lists here, what’s expected to follow immediately after the variable variable_X (instead of the opening text quotation mark found) is…
Moving on, I agree with James that a tell FMP statement would normally be implicit in running a script from within FileMaker. (This was certainly my experience when scripting in FMP 3 & 4 “ and possibly even earlier.)
So I’d be rather surprised if something like this didn’t work:
set {oldName, newName} to {cell "old file name", cell "new file name"} of current record
tell application "Finder" to set item oldName's name to newName
(Incidentally, when using Finder to target the desktop, it shouldn’t be necessary to explicitly define a path to the desktop “ since that’s Finder’s default object.)
Okay, now that we’ve nailed down what works for a file on the Desktop, how would you change the script to point to a file in a folder somewhere else. For convenience sake, let’s say that the file is in folder ‘collect’ on the Desktop.
tell application "Finder" to set deskPath to (path to desktop folder from user domain) as string
tell application "FileMaker Pro" to set {oldName, newName} to {cell "old file name", cell "new file name"} of current record
tell application "Finder" to set name of alias (deskPath & "collect:" & oldName) to newName
I thought (from the code that Charlie tested) we’d established that explicit FMP tell statements aren’t necessary, James.
So I was going to suggest a couple of alternatives that still avoid sandwiched tells: Using a Finder reference:
set {oldName, newName} to {cell "old file name", cell "new file name"} of current record
tell application "Finder" to set name of file oldName of folder "collect" to newName
Defining a file path:
set {oldName, newName} to {cell "old file name", cell "new file name"} of current record
tell application "Finder"
set filePath to (path to desktop folder as Unicode text) & "collect:" & oldName
set file filePath's name to newName
end tell