Jump to content

Passing a FMP variable to an applescript module


This topic is 4044 days old. Please don't post here. Open a new topic instead.

Recommended Posts

I am executing this attached script within FileMaker Pro (FMP) and it works correctly. However, there is one change I want to make. I am now introducing the usage of FMP layout.

I want to replace in my script the command "set {text returned:theEVE, button returned:buttonReturned} to display dialog "Please enter your event numbers (i.e 1111 2222) :" buttons { "Changed Invoice No", "OK"} default button 2 default answer theEVE" by using this FMP layout (see file attached).

The variable to validate.e to run through this script is called "OrdeEvent1".

What is the syntax in FMP script to pass a FFM variable to an applescript module. In this example, the variable theEVE is to get the value of "OrderEvent1".

Thanks again !

Daniel

set theEVE to ""

tell me to activate

sset {text returned:theEVE, button returned:buttonReturned} to display dialog "Please enter your event numbers (i.e 1111 2222) :" buttons { "Changed Invoice No", "OK"} default button 2 default answer theEVE



set errorLength to 0

set longEVE to length of theEVE

if longEVE = 4 or longEVE = 9 or longEVE = 14 or longEVE = 19 or longEVE = 24 or longEVE = 29 or longEVE = 34 or longEVE = 39 or longEVE = 44 or longEVE = 49 then

	set ElementOK to true

else

	set errorLength to 1

end if



set errorInvalidCharacter to 0

set chars to every character of (longEVE as string)

repeat with i from 1 to length of chars

	if "!$&\"'*(){[|;<>?~` \\,." contains (item i of chars as text) then

		set errorInvalidCharacter to 1

	end if

end repeat

FMP-FORUM - Validate User Entry.tiff

Link to comment
Share on other sites

You cannot pass a FileMaker script Variable to AppleScript. It simply cannot see them. So you need to put the value into a field (regular or global), which AppleScript can see, if either the field is on the current layout, or if you specify its location (via either layout name or table (occurrence) name. Global fields do not require the "of current record" phrase, regular fields do.

There are two errors in your AppleScript. "sset" in the dialog line (likely just a typo). But also, you have the wrong AppleScript variable in the 2nd test, for illegal characters. You are only checking the "length", not the real answer. This is the fix:

set chars to every character of (theEVE as string) -- was "longEVE", the length

Also, you're not doing anything with the two tests, if there is an error. But I imagine that is just not in the posted script.

Optionally, you could do this much the same using native FileMaker funtions. The first test could use Length (global entry field); the second test, Filter (field; all allowed characters), or Filter (field; not allowed characters), ie., positive or negative test.

Link to comment
Share on other sites

You absolutely can pass FMP variables to Applescript, you must use the Perform Applescript of course and choose calculated Applescript. Define the variables you want to pass to Applescript in a Let function then simply place the Applescript in the calculation portion of the Let function with the variables you defined. Make sure to contain your Applescript in quotes and escape your FMP variables.

Link to comment
Share on other sites

I find it much easier to place the applescript in a field, where it can be simple straight applescript without the complex escaping and quoting. Then do the substitutions you describe.

But I agree with Fenton - there is no need to use applescript at all for this, it will be easier with standard Filemaker scripting.

Link to comment
Share on other sites

Thank you Fenton for finding the error with theEvent, I really missed this one.

My intention is to perform all of those validations using native FileMaker functions. Unfortunately, I am new to FileMaker Pro and even if I look at different books I acquired ( FileMaker Pro 10 - Bible, FileMaker Pro - The missing manual, File Maker Pro - In depth) I find these books are not providing me with sufficient examples of scripting.

This is why I've created an applescript module which can perform the required operations because I am more familiar with applescript programming. I was then hoping to be able to pass those values and let the applescript module to display a message as if the entry is correct or not. This script is being triggered "On mode exit".

I still cannot figure out how to code this using FileMaker native functions. Where could I find a lot of examples ?

Best regards!

Daniel

set theEVE to ""

tell me to activate

set {text returned:theEVE, button returned:buttonReturned} to display dialog "Please enter your event numbers (i.e 1111 2222) :" buttons {"Changed Invoice No", "OK"} default button 2 default answer theEVE



--verify if the length of string is 4, 14, 19...

set errorLength to 0

set longEVE to length of theEVE

if longEVE = 4 or longEVE = 9 or longEVE = 14 or longEVE = 19 or longEVE = 24 or longEVE = 29 or longEVE = 34 or longEVE = 39 or longEVE = 44 or longEVE = 49 then

	set ElementOK to true

else

	set errorLength to 1

end if



--verify if the string contains invalid characters

set errorInvalidCharacter to 0

set chars to every character of (theEVE as string)

repeat with i from 1 to length of chars

	if "!$&\"'*(){[|;<>?~`\\,." contains (item i of chars as text) then

		set errorInvalidCharacter to 1

	end if

end repeat



--parse the string and ensure it it always separated by a blank

set theEVEclient to {}

set theStart to 1

set theEnd to 4

set PositionBLank to 0

set errorSeparator to 0

set errorFormat to 0

set EVElength to length of theEVE

repeat while (theEnd is less than or equal to length of theEVE)

	set theEVEclient to theEVEclient & (text theStart thru theEnd of theEVE)

	set theStart to theStart + 5

	set theEnd to theEnd + 5

	set PositionBLank to theStart - 1

	if PositionBLank is less than EVElength then

		if (character PositionBLank of theEVE) is not equal to " " then

			set errorSeparator to 1

		end if

	end if

end repeat



--verify if all parsed values are numeric

set cntOrder to 0

set listErrorFormat to ""

repeat with valeur in theEVEclient

	set cntOrder to cntOrder + 1

	try

		valeur as integer

	on error

		set errorFormat to 1

		set listErrorFormat to listErrorFormat & valeur & " "

	end try

end repeat



--display a message as if user entry is valid or not.

if errorLength is equal to 0 and errorSeparator is equal to 0 and errorFormat is equal to 0 and errorInvalidCharacter is equal to 0 then

	display dialog "no error"

else

	display dialog "To be valid, entered number must numeric, 4 characters long and separated by a blank"

end if

Link to comment
Share on other sites

Your FileMaker installation has loads of example files; and a tutorial.

The books you describe cover scripting as well.

Doing this in applescript is by far the long way around in this case; and you'll need to learn FileMaker calculations anyway to use FileMaker effectively.

If you can't do this in FileMaker, then it suggests that there are a lot of other basic calculations and native FileMaker features that you are not taking advantage of.

Link to comment
Share on other sites

I just wanted to make it clear that you can pass variables to Applescript because it is often posted that you can't.

It is often posted that you can't, because it's true. Calculating the text of the script before it runs is not the same as passing a variable to a script at runtime.

Link to comment
Share on other sites

Why not pass at runtime. You could create a Filemaker script that gets all variables being used and just call the script from Applescript. They will be available to an Applescript with out initial execution from Filemaker. There is more you can do. Its just not made for you already but the ability is there. I don't know what does pass a variable mean anyway?

Link to comment
Share on other sites

Why not pass at runtime.

Because you can't. All the workarounds you have mentioned work around this exact point.

I don't know what does pass a variable mean anyway?

It means you either tell script to run with a parameter (as you can do with a Filemaker script) or you create a variable that the script can read at runtime. In both cases, the same script behaves differently based on the passed value. As Fenton mentioned earlier, the only way you can do this with Applescript is by placing the value inside a field.

Link to comment
Share on other sites

In this post what I am trying to do is to remove from my applescript program the statement

set {text returned:theEVE, button returned:buttonReturned} to display dialog "Please enter your event numbers (i.e 1111 2222) :" buttons {"Changed Invoice No", "OK"} default button 2 default answer theEVE

The information to be validated from this this script is the value contained in my layout under a field named Order1.

What I need is to take the value of Order1 and moved it to variable theEVE.

I've started to use native FMP functions as recommended.

Link to comment
Share on other sites

First, sorry to imply that you cannot pass FileMaker $variables to AppleScript. I should have said, "if your AppleScript code is Native, you cannot pass them directly". I kind of assumed a longish script such as he had would have to be native. I never use the calculation option for long AppleScripts. But yes, global $$variables can be included in a calculated AppleScript.

As far as passing a regular field's value to AppleScript. If the field "Order1" is on the current layout (simplest), near the top of the AppleScript, before using theEve (duh).

set theEve to cell "Order1" of current record

If you write this within AppleScript Editor, then the above line would need to be in a: tell application "FileMaker Pro" block. But that is not needed with FileMaker Perform AppleScript (it is implied). Just comment it out before copy/pasting to the FileMaker Perform AppleScript.

Link to comment
Share on other sites

But yes, global $$variables can be included in a calculated AppleScript.

Strictly speaking - they can't. They can be included in a calculation that returns the text of the AppleScript. The actual Applescript being run includes only the extracted values. I believe the difference is more than merely semantics.

Link to comment
Share on other sites

In adding

set theEve to cell "Order1" of current record

to the Perform AppleScript function it worked exactly as wanted.

The applescript module get the value entered in Order1 and the program return an error message when there is an invalid entry. Since the script is executed with a Script Triggers set to OnObjectExit the information is then validated before the operator goes to make an entry or line #2 (Order2).

I will be executing the exact same script for the four Order lines. Then the user will have to click on a Commit button.

My next challenge will be to perform a specific operation when the user press the Commit button.

Each 4 digits order numbers (Line1, 2 3 and 4) will become a new record in the OrderClient table.

The OrderClient table contains an InvoiceNo, a LineNumber, OrderNo, a Status and a Date entry fields

I want to thank everyone of you who helped me resolving this issue.

Best regards!

Happy new year to everyone!

Daniel Paquin

N.B.: Comment, I will take the FMP dbms you've provided me and look at it more closely.

Link to comment
Share on other sites

Strictly speaking - they can't. They can be included in a calculation that returns the text of the AppleScript. The actual Applescript being run includes only the extracted values. I believe the difference is more than merely semantics.

Agreed, Comment

I use calculated Applescripts quite a lot to manage a dynamic multi-printer system. The most common command is "Do Shell Script" which effectively means FM is doing an Applescript which is calling an OS level routine. The important thing to remember is that all this is TEXT. It gets almost silly . . .what with the Quote function etc. It's all working, but each time I implement another instance of this sort of scripting, I wish there were a better way.

RW

Link to comment
Share on other sites

But a better way HAS been provided. Put the script in a text field; say in a resource table. No quoting necessary. Use place holders and substitution. Example:

Contents of field Resource::script:

do shell script "cd 'sourceDIR' ; ls - l > 'targetFILE'"

Perform Applescript:

substitute( resource::script; ["sourceDIR"; "~/documents"]; ["targetFile"; "~/desktop/docs.txt"])

Link to comment
Share on other sites

Because you can't. All the workarounds you have mentioned work around this exact point.

It means you either tell script to run with a parameter (as you can do with a Filemaker script) or you create a variable that the script can read at runtime. In both cases, the same script behaves differently based on the passed value. As Fenton mentioned earlier, the only way you can do this with Applescript is by placing the value inside a field.

Ok, I think I get it. When I am using Let ( ) I am really creating an Applescript variable.

Nevertheless you can get a Filemaker variable into Applescript without a global field. And if the Applescript did not launch from Filemaker you could create a Filemaker script that was called from Applescript containing variables as well.

Link to comment
Share on other sites

Nevertheless you can get a Filemaker variable into Applescript without a global field.

Yes, you can - at a price. The price is either dealing with a calculated (i.e. escaped ) Applescript, that isn't easy to read or modify or test in Script Editor - or keeping the text of the script in a global field and losing it when the file is cloned.

And if the Applescript did not launch from Filemaker you could create a Filemaker script that was called from Applescript containing variables as well.

You can do that even if the Applescript is called by Filemaker. But you still cannot pass a Filemaker variable to the Applescript (nor in the opposite direction).

Link to comment
Share on other sites

  • 1 year later...

I have a similar problem with getting a FM variable to work in a calculated applescript (I'm new to applescript and fairly new to FM). My task is simple in concept: to open a Finder window by clicking a button on the layout. The folder is located on a company server. Each project record in the database has a corresponding folder on the server where various documents are stored. The button triggers a script called OpenFolder which has one step: Perform Applescript (calculated).

"tell application "Finder"

activate

make new Finder window to folder "Let ([

text = GetAsText ( PROJECTS::directory_path);

FM_path = GetValue ( text; ValueCount (text) )

];

Substitute ( FM_path; ["/"; ":"] )

)"

select Finder window 1

end tell"

The problem I'm having is that I have to convert the FM path syntax contained in PROJECTS::directory_path to Applescript path syntax. I've tried to embed a Let function into the script to do this. However, it doesn't work. I think I'm messing-up the calculated applescript syntax because I don't entirely understand the quotation issue and usage. Used separately, the LET function works as intended (replaces all of the "/" with ":"), and the Applescript (shown below) works as intended (opens a finder window of the correct directory). It's only when I try to combine them that it fails.

tell application "Finder"

activate

make new Finder window to folder "projects:client:Job Number:"

select Finder window 1

end tell

I tried replacing the embedded LET function in the calculated applescript with a global variable ($$path), but it didn't work either. I'm at a loss.

Any help would be greatly appreciated.

Link to comment
Share on other sites

You have posted this in a thread that already has 20 posts - but it doesn't seem like you have read them carefully enough. A calculated AppleScript is a formula (in Filemaker syntax) that returns the text of the AppleScript to perform. If, at the end, you want the AppleScript to be:

tell application "Finder"

activate

make new Finder window to folder "projects:client:Job Number:"

select Finder window 1

end tell

then your calculation must return exactly the above text, as for example =

Let ( [

template = "tell application "Finder"¶activate¶make new Finder window to folder "myFolder"¶select Finder window 1¶end tell" ;

path = Substitute ( FM_path ; "/" ; ":" )

] ;

Substitute ( template ; "myFolder" ; path )

)

will - provided your FM_path field contains the text "projects/client/Job Number/".

Link to comment
Share on other sites

This topic is 4044 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.