Easy way to know if script is being run on the server

I have a script that I sometimes run locally and sometimes on the server. I can pass in a variable to know if it's on the server or not, but is there an easy way to test if script is being performed on a server or locally?

I use not a script to detect, but a process that performs script on server if there is FMServer hosting the files, otherwise perform local:

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

name: _template_PSoS

purpose:

description: Host is Server AND we're Client => PSoS Script "self", Exit, else => THE SCRIPT

dependencies:

used in Layout/Script: Universal

comments: set ahead of every Script

Author: EfficientBizz GmbH - Benjamin Fehr

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

If [ // Host is Server PatternCount ( Get(HostApplicationVersion) ; "Server" ) and // we're NOT on Server PatternCount ( Get(ApplicationVersion) ; "Server" ) = 0 ]
Set Variable [ $$progressbar ; Value: 1 ]
Refresh Window []
If [ Abs (Get(ApplicationVersion)) <17 ]
Perform Script on Server [ Specified: From list ; ā€œ_template_PSoSā€ ; Parameter: PSoS_coordinates_Set ( "Value" ) /* $Layout $FieldName $ID $Parameter $Value / ; Wait for completion: On ]
Else
Perform Script on Server [ Specified: By name ; Get ( ScriptName ) ; Parameter: PSoS_coordinates_Set ( "Value" ) /
$Layout $FieldName $ID $Parameter $Value */ ; Wait for completion: On ]
End If
Set Variable [ $$progressbar ; Value: 0 ]
Set Variable [ $$PSoS_Error ; Value: Get(ScriptResult) ]
Exit Script [ Text Result: ]
End If

If [ // - Server-Side - PatternCount ( Get(ApplicationVersion) ; "Server" ) ]
Set Variable [ $PSoS ; Value: PSoS_coordinates_extract /* $Layout $FieldName $ID $Parameter $Value */ ]
# Find
Go to Layout [ $Layout ; Animation: None ]
Enter Find Mode [ Pause: Off ]
Set Field By Name [ $FieldName ; $ID ]
Perform Find []
End If

THE SCRIPT - either Server OR Client

2 Likes

Question: Why fork the code for application version less than 17?

I can see that you are able to use "specify by name" for 17 and up, and that you can then call get(script name). That section of code is very portable - you can cut/paste and change the script name without having to modify the perform script call, but that isn't available for earlier versions.

My question is really why you choose to implement the new code? The code that works for versions less than 17 will work perfectly well for newer versions.

Thatā€˜s correct. Itā€˜s a template to copy/paste at the beginning of every script to build a hybrid to work either local OR as PSoS if an FMS Host is detected.
For the ā€žForkā€œ: I needed downward compatibility for a user running FMS16.
There ara 2 CustomFunctions involved to first write ā€žcoordinatesā€œ for current focus (Table, Layout, PrimaryKey) for second cf to resolve on server side (PSoS).

I have a script that I sometimes run locally and sometimes on the server. I can pass in a variable to know if it's on the server or not, but is there an easy way to test if script is being performed on a server or locally?

Hello @JasonMark

Many devs will use the output of the Get( ApplicationVersion ) function as a means to determine whether or not the code is currently running on the server.

Hope this helps solve what you are after.

-steve

1 Like

Thanks, I saw this in the original script and figured it's the way to do it. Thanks all!

1 Like