Saturday, August 8, 2020

#1 2020-07-09 10:20:22 pm

DaMacGuy
Member
Registered: 2006-02-16
Posts: 4

Trying to write a shell script that uses osascrip and run from launchd

I've got a script that writes a launch daemon and a second shell script. When the second shell script is called by the launch daemon it mostly works, except for the osascript part.

Here's the function in the parent script that writes the shell script that has the osascript in it.

Applescript:

## CREATES the Script that lives on the machine until completed
createUpdateScript(){
cat << EOF > /private/var/tmp/resetManagement.sh
#!/bin/sh
exec >> "/var/log"/"scriptDebug.log" 2>&1
set -x
/bin/sleep 5
open -b com.apple.systempreferences /System/Library/PreferencePanes/Profiles.prefPane
osascript -e 'display dialog "Please select \"MDM Profile\"and then" & return & "click \"Approve\"." with icon file "Macintosh HD:System:Library:CoreServices:CoreTypes.bundle:Contents:Resources:Actions.icns" buttons {"OK"}'
exit 0
EOF
sudo chmod +x /private/var/tmp/resetManagement.sh
}

And in my log file I get this which I just do NOT understand...

+ /bin/sleep 5
+ open -b com.apple.systempreferences /System/Library/PreferencePanes/Profiles.prefPane
+ osascript -e 'display dialog "Please select \"MDM Profile\"and then" & return & "click \"Approve\"." with icon file "Macintosh HD:System:Library:CoreServices:CoreTypes.bundle:Contents:Resources:Actions.icns" buttons {"OK"}'
2020-07-10 00:05:48.135 osascript[34124:477439] -[__NSCFConstantString objectAtIndex:]: unrecognized selector sent to instance 0x7fff8c4e4f80
2020-07-10 00:05:48.136 osascript[34124:477439] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString objectAtIndex:]: unrecognized selector sent to instance 0x7fff8c4e4f80'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff34cd9be7 __exceptionPreprocess + 250
    1   libobjc.A.dylib                     0x00007fff6d9185bf objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff34d58c77 -[NSObject(NSObject) __retain_OA] + 0
    3   CoreFoundation                      0x00007fff34c3e44b ___forwarding___ + 1427
    4   CoreFoundation                      0x00007fff34c3de28 _CF_forwarding_prep_0 + 120
    5   CoreFoundation                      0x00007fff34c0cf67 CFArrayContainsValue + 197
    6   HIServices                          0x00007fff32f177b9 TransformProcessType + 927
    7   osascript                           0x000000010b21102c osascript + 12332
    8   HIToolbox                           0x00007fff3392a7c5 AEInteractWithUser + 53
    9   StandardAdditions                   0x000000010cdd58b9 CSDSTEXTurl_ + 5865
    10  StandardAdditions                   0x000000010cdc6331 AEVTsysodlog + 85
    11  AE                                  0x00007fff35fcf203 _AppleEventsCheckInAppWithBlock + 18103
    12  AE                                  0x00007fff35fdec83 AESendMessage + 2838
    13  AE                                  0x00007fff35feafb1 aeSend + 355
    14  osascript                           0x000000010b20fb25 osascript + 6949
    15  AppleScript                         0x00007fff48940342 _Z13ComponentSendPK6AEDescPS_ii + 485
    16  AppleScript                         0x00007fff48951f0a _ZN15TUASApplication4SendEP25TStackFrame_UASRemoteSendP6AEDescS3_hhh + 2332
    17  AppleScript                         0x00007fff48972ca7 _Z13UASRemoteSendhhhhhPh + 548
    18  AppleScript                         0x00007fff4894b1f0 _Z13UASActor_Sendhhh + 383
    19  AppleScript                         0x00007fff4897e402 _Z13UASValue_Sendhh14TUASClassIndexh + 326
    20  AppleScript                         0x00007fff48955ff9 _Z11UASExecute1v + 288
    21  AppleScript                         0x00007fff48929b26 _Z14ASExecuteEventPK6AEDescjiPj + 614
    22  AppleScript                         0x00007fff48922971 AppleScriptComponent + 1677
    23  AppleScript                         0x00007fff4893bbdb _ZN12AGenericCall8DelegateEP23ComponentInstanceRecord + 37
    24  AppleScript                         0x00007fff4893bba1 _ZN15AGenericManager13HandleOSACallEP19ComponentParameters + 57
    25  AppleScript                         0x00007fff4893b18c GenericComponent + 156
    26  OpenScripting                       0x00007fff33b6d3e2 OSAExecuteEvent + 50
    27  osascript                           0x000000010b2105bd osascript + 9661
    28  libdyld.dylib                       0x00007fff6eabfcc9 start + 1
    29  ???                                 0x0000000000000003 0x0 + 3
)
libc++abi.dylib: terminating with uncaught exception of type NSException
/private/var/tmp/resetManagement.sh: line 6: 34124 Abort trap: 6           osascript -e 'display dialog "Please select \"MDM Profile\"and then" & return & "click \"Approve\"." with icon file "Macintosh HD:System:Library:CoreServices:CoreTypes.bundle:Contents:Resources:Actions.icns" buttons {"OK"}'
+ exit 0


These errors boggle my mind. It's a simple display dialog!

If I run this manually (sudo /private/var/tmp/resetManagement.sh) it works.

(running macOS 10.15, which isn't an option in the posting)

Browser: Safari 605.1.15
Operating System: Other

Offline

 

#2 2020-07-09 11:35:40 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6434

Re: Trying to write a shell script that uses osascrip and run from launchd

What happens if you remove the icon code? You probably don't have permission to read files in /System/Library.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2020-07-10 09:54:15 am

DaMacGuy
Member
Registered: 2006-02-16
Posts: 4

Re: Trying to write a shell script that uses osascrip and run from launchd

Good thought... but no. Didn't work. Similar/same error.

I also made sure I'm using the full path to the osascript command as someone else reminded me that launchd doesn't have the same paths mapped.

But that's not working. I've even made the script stupid easy. It's just writing these two lines and then running the script.

#/bin/bash
/usr/bin/osascript -e 'display dialog "Please select DNG MDM Profile and then click Approve."'



That gives me a big error like before...

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString objectAtIndex:]: unrecognized selector sent to instance 0x7fff8c4e4f80'



So I decided to make it even SIMPLER!

#/bin/bash
/usr/bin/osascript -e 'tell application "System Preferences" to activate'



And THAT WORKS!

So the problem is just with display dialog???

Offline

 

#4 2020-07-10 06:23:39 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6434

Re: Trying to write a shell script that uses osascrip and run from launchd

DaMacGuy wrote:

So the problem is just with display dialog???



More likely with any UI. Can your launch daemon show any UI?


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2020-07-31 10:06:16 am

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 335

Re: Trying to write a shell script that uses osascrip and run from launchd

A couple of ideas that may or may not help...

1) If the launch daemon is stumbling on a user interface issue, you could try saving the AppleScript command as an AppleScript application and then opening the application. Save the following command as an application, for example on the desktop at /Users/[your home folder name]/Desktop/Test.app:

Applescript:


display dialog "Please select \"MDM Profile\"and then" & return & "click \"Approve\"." with icon file "Macintosh HD:System:Library:CoreServices:CoreTypes.bundle:Contents:Resources:Actions.icns" buttons {"OK"}

or, alternatively, with the display dialog command wrapped in an application:

Applescript:


tell application "System Events"
   activate
   display dialog "Please select \"MDM Profile\"and then" & return & "click \"Approve\"." with icon file "Macintosh HD:System:Library:CoreServices:CoreTypes.bundle:Contents:Resources:Actions.icns" buttons {"OK"}
end tell

Then, replace the osascript command in your shell script with the following:

Applescript:


open -a '/Users/[your home folder name]/Desktop/Test.app'

2) This is a minor point, but in your original post, the shebang line is correct (#!/bin/sh), but in your subsequent examples, the exclamation point is missing.  Just wanted to be sure that it is correct in your actual script.

Last edited by bmose (2020-07-31 10:07:24 am)

Offline

 

#6 2020-07-31 01:39:22 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 332

Re: Trying to write a shell script that uses osascrip and run from launchd

Little search on google and I find his for you, maybe it will be helpful.

https://jerbecause.wordpress.com/2018/0 … ing-uamdm/

Good luck.


The best knowledge is always free, we share ideas, thoughts and expressions. So we could build better worlds together.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)