Problems running a script "as user" in policy and using file with spaces in command

dlondon
Valued Contributor

I'm trying to write a generic script to set default app for a document type e.g. PDF using https://github.com/Lord-Kamina/SwiftDefaultApps/releases

This works from a policy where $4 is set to: /Applications/Nitro\ PDF\ Pro.app
and $5 is set to com.adobe.pdf - but in the script I explicitly ignore $4 and set application_name

 

 

#!/bin/bash
# SetDefaultApplication.bash
#application_name="$4"
application_name=/Applications/Nitro\ PDF\ Pro.app
echo $application_name
# e.g. /Applications/Nitro\ PDF\ Pro.app
uti_name="$5"
# e.g. com.adobe.pdf

currentUser=$(stat -f%Su /dev/console)

# setHandler
sudo -u $currentUser /usr/local/libexec/swda setHandler --application "$application_name" --UTI $uti_name

 

 

 

and in the policy I see the output as:

Script result: /Applications/Nitro PDF Pro.app
SwiftDefaultApps SUCCESS: Default handler has succesfully changed to com.gonitro.NitroPDFPro

 

But, if I try and use $4

 

 

#!/bin/bash
# SetDefaultApplication.bash

application_name="$4"
#application_name=/Applications/Nitro\ PDF\ Pro.app
echo $application_name
# e.g. /Applications/Nitro\ PDF\ Pro.app
uti_name="$5"
# e.g. com.adobe.pdf

currentUser=$(stat -f%Su /dev/console)

# setHandler
sudo -u $currentUser /usr/local/libexec/swda setHandler --application "$application_name" --UTI $uti_name

 


I get an error from the policy

Script result: /Applications/Nitro\ PDF\ Pro.app
SwiftDefaultApps ERROR -10814: No application found for /Applications/Nitro\ PDF\ Pro.app

 

and yet on that machine, I can run the script as root

dep54592:lab root# ./SetDefaultApplication.bash 1 2 3 /Applications/Nitro\ PDF\ Pro.app com.adobe.pdf
/Applications/Nitro PDF Pro.app
SwiftDefaultApps SUCCESS: Default handler has succesfully changed to com.gonitro.NitroPDFPro


Just wondering if anyone can see where I'm going wrong?

 

3 ACCEPTED SOLUTIONS

sdagley
Esteemed Contributor II

@dlondon Are you using /Applications/Nitro\ PDF\ Pro.app or just /Applications/Nitro PDF Pro.app as Parameter 4 when calling your script from Jamf Pro? If the former, what happens when you try the latter?

View solution in original post

mm2270
Legendary Contributor III

When using application_name="$4" in the script, it implies that the variable would be passed as double quoted, in which case you don't want to use a string for $4 using the backslashes. "/Applications/Nitro\ PDF\ Pro.app" is not a valid path and the error is indicating as such. So, basically the same thing that @sdagley is implying in their post.

View solution in original post

dlondon
Valued Contributor

So here's a cleaner copy of the final script with credits and explanations

#!/bin/bash
# SetDefaultApplication.bash
# David London
# 2022-09-01
# With help from sdagley (Steve Dagley) and mm2270 (Mike Morales) 
#  in https://community.jamf.com/t5/jamf-pro/problems-running-a-script-quot-as-user-quot-in-policy-and-using/m-p/272752#M248439
# Initial direction from the end of this post on using swda https://community.jamf.com/t5/jamf-pro/setting-default-open-with-app/m-p/11473
# This is a generic script that uses the command line component of SwiftDefaultApps 
#  available at https://github.com/Lord-Kamina/SwiftDefaultApps/releases
# This script will set the default app for a file type for the currently logged in user
# The script is intended to be used in a policy and a copy of the command line component of SwiftDefaultApps has been placed in a known location
#  In this case the known location of swda is /usr/local/libexec/ but it could be anywhere e.g. /tmp
# For me, I just package up swda in /usr/local/libexec and deploy that first before the script in the policy

# grab $4 e.g. /Applications/Nitro PDF Pro.app
application_name="$4"

# grab $5 e.g. com.adobe.pdf
uti_name="$5"

# get the current user
currentUser=$(stat -f%Su /dev/console)

# setHandler
sudo -u $currentUser /usr/local/libexec/swda setHandler --application "$application_name" --UTI $uti_name


and in Jamf Pro the parameter tab looks like this

Screen Shot 2022-09-01 at 9.38.13 am.pngT

 

View solution in original post

3 REPLIES 3

sdagley
Esteemed Contributor II

@dlondon Are you using /Applications/Nitro\ PDF\ Pro.app or just /Applications/Nitro PDF Pro.app as Parameter 4 when calling your script from Jamf Pro? If the former, what happens when you try the latter?

mm2270
Legendary Contributor III

When using application_name="$4" in the script, it implies that the variable would be passed as double quoted, in which case you don't want to use a string for $4 using the backslashes. "/Applications/Nitro\ PDF\ Pro.app" is not a valid path and the error is indicating as such. So, basically the same thing that @sdagley is implying in their post.

dlondon
Valued Contributor

So here's a cleaner copy of the final script with credits and explanations

#!/bin/bash
# SetDefaultApplication.bash
# David London
# 2022-09-01
# With help from sdagley (Steve Dagley) and mm2270 (Mike Morales) 
#  in https://community.jamf.com/t5/jamf-pro/problems-running-a-script-quot-as-user-quot-in-policy-and-using/m-p/272752#M248439
# Initial direction from the end of this post on using swda https://community.jamf.com/t5/jamf-pro/setting-default-open-with-app/m-p/11473
# This is a generic script that uses the command line component of SwiftDefaultApps 
#  available at https://github.com/Lord-Kamina/SwiftDefaultApps/releases
# This script will set the default app for a file type for the currently logged in user
# The script is intended to be used in a policy and a copy of the command line component of SwiftDefaultApps has been placed in a known location
#  In this case the known location of swda is /usr/local/libexec/ but it could be anywhere e.g. /tmp
# For me, I just package up swda in /usr/local/libexec and deploy that first before the script in the policy

# grab $4 e.g. /Applications/Nitro PDF Pro.app
application_name="$4"

# grab $5 e.g. com.adobe.pdf
uti_name="$5"

# get the current user
currentUser=$(stat -f%Su /dev/console)

# setHandler
sudo -u $currentUser /usr/local/libexec/swda setHandler --application "$application_name" --UTI $uti_name


and in Jamf Pro the parameter tab looks like this

Screen Shot 2022-09-01 at 9.38.13 am.pngT