Apple script to run a shell script to get around permissions - macos

I have created a shell .command on Mac OS X that I would like to distribute. Based on my testing, you can't execute the .command file by double clicking without changing permissions first ( making executable. ) I don't want users to use the terminal and change permissions, it's too hard for them.
The shell script creates a folder structure for a project, based on where the script is. It needs to be able to run anywhere the user puts it.
The research I have done indicates that I need to use an Apple script to run the file to prompt for the user's password to get around permissions.
Any advice on how to do this?

Distribute the file either by zip or DMG - props Thilo.

Related

Ubuntu Linx, how to run a shell script with a specific users PATH, without having to change .bashrc file?

I want to dynamically create a user account from a shell script (which runs while my device boots up) and run some more shell scripts under the new user's account. Right now I am using the below command to run the shell scripts under a specific user account.
su -c "eval $shellscript.sh" -s /bin/sh **newuser**
This is working to the extent that, shell scripts are actually running under the user name newuser. However, I am facing another problem.
The newuser's PATH variable is initialized with /usr/bin:/bin: However shellscript.sh uses some commands (like ifconfig etc.,) which are not present in the above folders. Hence these commands are failing.
I am trying to figure out how to add the required folders to the PATH variable of "newuser" without modifying the file ./~profile of newuser, and also while making sure that PATH gets updated only for this user but not for the entire system.
In a nutshell I want to update the PATH variable of newuser dynamically from a shell script and make some other shellscript use this PATH variable.
is there any such possibility in Ubuntu Linux?

Change Windows File permissions in Lua to allow writing

I am reading and then modifying some data in a file that is located in the Windows program files folder.
Because of this the file does not have write permissions. So I receive an error when trying to write to it.
When manually giving write permissions to the folder containing the file, the script will work.
Is it possible to modify a folders permissions within a Lua script, or write to a file regardless of it's permissions? Or can this only be done manually from the File Explorer?
This is my current section of code that is re-writing the inputFile:
k=0
updatefile = io.open(inputFile,"w")
for k=1,i do
updatefile:write(modfilecontent[k] .. '\n') --This is where I'm blocked by permissions
end
updatefile:close()
Also if there is a solution possible, and I load this script onto another persons computer that does not have admin privileges would the solution still work?
EDIT:
Piglet's solution will work using the os.execute method to change privileges.
As long as the software, that the plug-in is being used in, is ran as admin the first time the permissions will be set. From then on you no longer need to run as admin.
Below is the code that I needed to add to the beginning of the program, minus the brackets of course
os.execute('icacls "C:\\Program Files\\[software name]\\[folder name]" /grant Everyone:(OI)(CI)F /T')
You could use os.execute to run the appropriate Windows command.
See How to grant permission to users for a directory using command line in Windows?
Alternatively use a library that supports stuff like this. Maybe checkout
https://keplerproject.github.io/luafilesystem/manual.html
Also if there is a solution possible, and I load this script onto
another persons computer that does not have admin privileges would the
solution still work?
As that person wouldn't be able to run Lua as administrator that is very unlikely. That would completly defy the purpose of different user levels.

How do I present application with additional server mode on macOS to customer

I have a desktop application that I successfully package and install on macOS using dmgcanvas. The user simply drags the icon into Applications to install and then run by clicking on the Application Icon in Applications.
My problem is whereas the software was previously a Desktop only application it now has a new mode whereby it runs as a server, and can then be controlled via a web browser. On Windows, I would simply create another .exe file with the option set to run as server and put this in the installation folder, so the user would just run MyApp.exe or MyAppServer.exe
But in macOS I can not see how to do the equivalent thing since there is just one folder Myapp.app containing the installation and clicking on Myapp.app runs the application, so where do I put MyAppServer?
Of course, the user could right-click on MyApp.app and run Show Package Contents and then navigate to a subfolder such as bin containing a cmdline version that runs in server mode. But how is the user supposed to know how to do that, I want an easy way for the user to run MyAppServer?
Its hard for find documentation of how things exactly work on MacOS. But below is a good article
https://mathiasbynens.be/notes/shell-script-mac-apps
The internal folder structure may vary between apps, but you can be sure that every Mac app will have a Contents folder with a MacOS subfolder in it. Inside the MacOS directory, there’s an extension-less file with the exact same name as the app itself. This file can be anything really, but in its simplest form it’s a shell script. As it turns out, this folder/file structure is all it takes to create a functional app!
They have a simple script to automate the whole process
#!/usr/bin/env bash
APPNAME=${2:-$(basename "${1}" '.sh')};
DIR="${APPNAME}.app/Contents/MacOS";
if [ -a "${APPNAME}.app" ]; then
echo "${PWD}/${APPNAME}.app already exists :(";
exit 1;
fi;
mkdir -p "${DIR}";
cp "${1}" "${DIR}/${APPNAME}";
chmod +x "${DIR}/${APPNAME}";
echo "${PWD}/$APPNAME.app";
So what I would do is that, instead of having my apps binary directly, I will have a Shell Script and the Binary in the same folder. This shell script will check if the AppName Server.app already exists or not, if not, it will create the folder and put a script which then calls your original app with -r flag.
And then I would just run the actual app with the pass arguments that were passed to the script
Is this the best approach? Not sure about that, I could not find a way where a app has multiple Icons in but just one folder. In this case the Server option will not exists on first installation of the app, but after the first run of the app. You could even ask the user if they really want an additional icon for the app or not.

On Mac OSX how can I associate a script file with Terminal so double clicking the icon launches Terminal?

I have a python script that I can run in the usual way using Terminal. For example, launch Terminal, cd to the directory, then type ./xxx.py where xxx.py is the name of the file that contains the script.
Now I want to make an icon on the desktop that launches Terminal and runs the python file when I double click it. How do I do that? I thought I could make a shell file with the cd and the launch command and then associate that shell file with Terminal. But I can't seem to associate it with Terminal.
Somebody said to name the shell file with the suffix '.command' but that causes it to launch Flash Builder. I don't know where that associate is set. I can't manually associate Terminal because I can't find it. It isn't in /Applications.
On your Desktop you coul create a file with the following content
#!/bin/bash
python PATH_TO_YOUR_PYTHON_FILE
Then you must make it executable via
chmod u+x FILE_ON_DESKTOP
Alternatively you can create a symbolic link to your python file and make it executable.
You can find Terminal in /Applications/Utilities

Making an executable bash file run when clicked

I have a bash file that does some file manipulation. I don't want to have to open the terminal every time I run it. Is there a way to make the program run when I double click it? (Like a windows .exe file)
Thanks
You can add a ".command" extension to the filename -- then double-clicking it will automatically open Terminal and run the script in a new window. Note: this assumes you still want to watch/interact with the script via a Terminal interface; if you want to avoid this as well, wrapping the script with Platypus, AppleScript, or Automator (as Zifei and Ned suggest) would be better options.
What you need is Platypus.
Platypus is a developer tool for the Mac OS X operating system. It can be used to create native, flawlessly integrated Mac OS X applications from interpreted scripts such as shell scripts or Perl and Python programs. This is done by wrapping the script in an application bundle directory structure along with an executable binary that runs the script.
The easiest thing to do is to type: sudo chmod 755 the_file_Name.This will allow you to double click on the file in the finder.
With OS X 10.5+, you can wrap the bash shell script in an AppleScript application using the AppleScript editor or an Automator application using Automator.app (see the Automator on-line help).
You could write (and there are apps out there that do this) an OS X app that accepts arbitrary .sh files and executes them. However, that's generally a bad idea as it could open you up to attacks if you inadvertently download a malicious shell script file that is automatically opened by your web browser. Better to be explicit.

Resources