Crontab not executing after code-signing in Mac - objective-c

I create cronJob in my mac application using apple script. It was working fine until codeSigning the application.
I had to code sign the application to keep away from gateKeepers. Each time i update the cron there was a gatekeeper popup. To remove gateKeepers i had to codeSign my application.
I used Developer ID Application certificate to codesign the application.
NSDictionary *error1 = [NSDictionary new];
NSString *script1 = #"tell application \"Terminal\" to do script \" crontab -l > mycron ; echo '* * * * * cd /Applications/PPFolder && ./CronCheck.sh >> /Applications/PPFolder/scriptlogs.log' >> mycron ; crontab mycron ; rm mycron ; exit;\" ";
NSAppleScript *appleScript1 = [[NSAppleScript alloc] initWithSource:script1];
if ([appleScript1 executeAndReturnError:&error1]) {
NSLog(#"success!");
} else {
NSLog(#"failure!");
}
After CodeSigning. It only works on my machine and not on other machine. Is it due to code signing or does other mac requires extra permissions.
I tried CodeSigning the application. Post that it stoped working on other machines. I need to get the cron working on all machines any suggestions as to what is going wrong.

Related

Can't run Apple script from command-line application

I created a command-line application for Mac OS without the GUI. This application is located at /usr/local/bin. And in some cases I need to execute Apple Script within that application. To do this, I create an NSTask and trying to run the following command:
NSTask *createTask = [[NSTask alloc] init];
createTask.launchPath = #"/bin/bash";
NSString *showAlert = [NSString stringWithFormat:#"/usr/bin/osascript -e 'tell application \"Finder\" to display alert \"My text.\"'"];
NSArray *arguments = [NSArray arrayWithObjects:#"-c",showAlert, nil];
createTask.arguments = arguments;
[createTask launch];
After it runs, nothing happens, only in logs appear the message:
Apr 14 15:35:15 Mac-mini kernel[0]: my-app: guarded fd exception: fd 0 code 0x2 guard 0x7fff8b9e12a8
Apr 14 15:35:15 Mac-mini com.apple.xpc.launchd[1] (com.apple.ReportCrash.Root[26852]): Endpoint has been activated through legacy launch(3) APIs. Please switch to XPC or bootstrap_check_in(): com.apple.ReportCrash.DirectoryService
Apr 14 15:35:15 Mac-mini diagnosticd[16097]: error evaluating process info - pid: 26851, punique: 26851
Apr 14 15:35:16 Mac-mini sandboxd[16099] ([26851]): my-app(26851) deny file-read-data /
But if you run this command directly from terminal, it is executed correctly. Please tell me, what am I doing wrong?
I think the issue may be with your use of quotes. When I try and manually run the same command in the shell with your quoting style, it won't work. My examples below do work. Can you switch your single and double quotes? Encase your initial call with single quotes and then use the double quote around the osascript? Also, there is no need to use the tell application \"Finder\" to as display alert is not part of the Finder dictionary.
You have...
/usr/bin/osascript -e 'tell application \"Finder\" to display alert \"My text.\"'
Try changing it to...
/usr/bin/osascript -e "display alert \"My text.\""
Or an even simpler version...
osascript -e "display alert \"My text.\""

how can I remove XPCService by script

I create project for os x application with xpcservices that run by loginItems. that's mean the service is founded in the app in the path:{APP_NAME}.app/Contents/Library/LoginItems and who is responsible for run the service is the the main app:
NSXPCConnection *connection = [[NSXPCConnection alloc] initWithLoginItemName:#"{SERVICE_NAME}.app" error:&error];
I builded uninstaller, for deleting all the files that I installed. and I need to remove the service.
the command I used for it :
launchctl remove SERVICE_NAME
this is works fine if I run it from the terminal,
but if i run it from script it's remove the service from the "Activity Monitor" but the service still in launchctl list with status -5.
so if I install new version its take the previous version that installed.
I run the script from xcode by NSTask. there is no error. it's does what the script should do.
I even set the administrator privileges:
/usr/bin/osascript -e 'do shell script \"SCRIPT_NAME args 2>&1 etc\" with administrator privileges'
Why it's not remove like I did it in the terminal ?
p.s:
its works when run it from Xcode (even the app is in release mode)
To remove correctly the login item you should do it in the code :
pass false in SMLoginItemSetEnabled:
SMLoginItemSetEnabled((__bridge CFStringRef)loginItemBundleId, false)

Run ShellScript Without Authorization Popup for MAC Application

I am working on MAC Application in which I want to Remove Helper tool previously installed by my application.
I am using STPrivilegedTask to run my ShellScript.
Here is the code I am using to achieve this.
NSString *pathForUninstallFile = [[NSBundle mainBundle] pathForResource:#"Uninstall" ofType:#"sh"];
STPrivilegedTask *taskToRemoveHelperTool = [STPrivilegedTask launchedPrivilegedTaskWithLaunchPath:pathForUninstallFile arguments:[NSArray arrayWithObjects:#"Uninstall.sh", nil]];
and here is my ShellScript Unistall.sh
launchctl unload /Library/LaunchDaemons/com.bsecure.HelperTool.plist
rm /Library/LaunchDaemons/com.appleCompany.HelperTool.plist
rm /Library/PrivilegedHelperTools/com.appleCompany.HelperTool
security -q authorizationdb remove "com.appleCompany.readLicenseKey"
security -q authorizationdb remove "com.appleCompany.newMethod"
Now the above code works perfect but it ask user's Authorization Popup.
I want to run this script without this Authorization Popup.
I already search in another question but none of them work for me that's why I created this question.
So you want to run a task with escalated privileges without prompting the user for a password? If so, you're out of luck, it can't be done.

Finder Sync Badge Extension Handling

I am trying to create a Finder Sync Extension,in yosemite to show badge in files and folder.
I am on the move, but i have no idea how to turn off extension(remove from extensions list in preferences) when my containing application terminate. Any help is appreciated.
Try it..
Reload Directory in Finder
// Reload Finder (change the word directory to file if updating file)
NSAppleScript * update = [[NSAppleScript alloc] initWithSource:[NSString stringWithFormat:#"tell application \"Finder\" to update POSIX directory \"%#\"",path]];
[update executeAndReturnError:nil];
Code to Enable Extension (bundle ID)
system("pluginkit -e use -i com.xyz.finderExt")
Code to Disable Extension (bundle ID)
system("pluginkit -e ignore -i com.xyz.finderExt")
code to remove from extensions list in preferences
pluginkit -r "/Applications/App-name/Contents/Plugins/extension-name.appex"

NSAppleScript with administrator previleges fails when application is launched from package maker

I have a MAC OSX cocoa application,where i used NSAppleScript to run some script with administrator privileges .The application works fine when it is launched manually or from any other script .
But NSAppleScript doesn't launch the intended script when i tried to launch the application from package maker .
I have made a postflight script to launch the application from package maker .
Postflight:
#!/bin/sh
open pathOFApp
NSAppleScript usage:
NSDictionary *error = [NSDictionary new];
NSAppleScript *appleScript = [[NSAppleScript new] initWithSource:#"do shell script \"pathOFScript \" with administrator privileges"];
if ([appleScript executeAndReturnError:&error]) {
NSLog(#"-----success --------");
}
else{
NSLog(#"-------Failure-----");
}
Please help to fix this .
I don't know the answer with applescript/PackageMaker, but have you tried using the unix security tool to launch your executable? I think it's been available since 10.5. You can probably combine this and your executable in one statement too.
/usr/bin/security execute-with-privileges /path/to/executable
Got the solution for this .Since postflight script is launched with the root permission by packageMaker it was creating some permission issue with the NSAppleScript . Launching the application as "sudo open MyApp" in postflight will resolve the issue.