Just simplyfing a process for some internal mac users to put files in the right places but having trouble with a plist file needing to go in /Library/LaunchDaemons. Using some applescript to accomplish this. Unfortunately (for me) LaunchDaemons is read only, so need to sudo root privilege to copy a file into there. I can't seem to get it right as to how to do that with applescript. Copying files to unrestricted locations is done just fine with something like
do shell script ("/bin/cp " & posix_path & "file_to_copy.crt" & "/path/to/folder/")
For the plist file in LaunchDaemons, ideally something like below would work,
do shell script ("sudo /bin/cp " & posix_path & "file_to_copy.plist" & "/Library/LaunchDaemons/") with administrator privileges
I've tried a lot of variations but no luck. Reading through stack overflow I haven't spotted a question quite like this. Any insight would be much appreciated!
As far as using sudo in a do shell script command, do not use sudo in a do shell script command, have a look at: Technical Note TN2065 do shell script in AppleScript
As far as /Library/LaunchDaemons/, it is not read-only! In macOS Big Sur, here are the permissions on a recent 11.4 build:
drwxr-xr-x 3 root wheel - 96 May 30 13:01 LaunchDaemons
Note that it is writable for root and why you need to use with administrator privileges when using the do shell script command to write to it.
Example AppleScript code
do shell script "cp '/path/to/filename.plist' '/Library/LaunchDaemons/'" with administrator privileges
The above do shell script command works for just fine me.
Note the single-quotes around the POSIX paths.
If you are not using hard coded POSIX paths and using variables and concatenating the do shell script command, you can use e.g, the quoted form of theFilename or theFilename's quoted form, etc.
Example AppleScript code
set theFilename to the POSIX path of (choose file)
set theDestinationFolder to "/Library/LaunchDaemons/"
do shell script "cp " & ¬
theFilename's quoted form & space & ¬
theDestinationFolder's quoted form ¬
with administrator privileges
Related
I have the following shell script that I normally run successfully as a system service (created in automator:
#!/bin/bash
for f in "$#"
do ln -s "$f" "${f}.deleteThisExtensionAfterMoving"
echo "${f}.deleteThisExtensionAfterMoving"
done
However, when trying to use this to make symlinks in folders that I'm not the owner of, it fails.
I tried saving it as a script, then using applescript in automator as I've seen described elsewhere here:
on run {input, parameters}
do shell script ("sudo ~/Documents/z_misc/makesymlink '" & POSIX path of input & "'") with administrator privileges
end run
However, upon running the service, I see a "command not found" error.
When I tried saving the script as a .sh file and using that instead, it still didn't work. When I try running from terminal without the preceding "sudo" command, I get a "permission denied" error.
When I try running from terminal without the preceding "sudo" command, I get a "permission denied" error.
The "permission denied" error when attempting to execute a shell script (or binary executable) is typically caused because the file is not set as executable. Use the following command in Terminal:
chmod +x file
Or:
chmod +x /path/to/file
Quoting if there are spaces or escaping spaces with a backslash (\), but not both.
However, the "permission denied" error can also be caused if there is a privileges issue with where the shell script or binary executable is located.
Keeping executables in you Documents folder or any of the default folders, except for Public, within your Home folder can be problematic to Automator actions and for use in Terminal.
I created a bin folder in my Home folder and place my shell scripts and binary executables that I do not want elsewhere, e.g. /use/local/bin, and have added "$HOME/bin" to my PATH.
Here is what I'd use in the Run AppleScript action in my Automator Service/Quick Action:
Example AppleScript code:
on run {input, parameters}
repeat with thisItem in input
set thisItem to the quoted form of the POSIX path of thisItem
do shell script ("$HOME/bin/makesymlink " & thisItem) with administrator privileges
end repeat
end run
As coded, this allows for multiple Finder items to be passed to the Automator Service/Quick Action.
That all said, without seeing the contest of the makesymlink file there is not anything else I can think off at the moment.
From the linked Technical Note below:
Note: Using sudo(8) with with administrator privileges is generally unnecessary and creates security holes; simply remove the sudo.
Have a look at Technical Note TN2065 do shell script in AppleScript
If you are going to use the do shell script command, then I suggest you read the entire Technical Note.
Essentially, what I am trying to accomplish is I want to cd into a specific directory containing all my lighthouse folders. I want to loop through each line of urls.txt and for every URL execute the npm lighthouse command and output the results into a CSV to analyze later.
do shell script "cd /Users/user/Documents/Lighthouse\\"
set srcFile to "urls.txt"
set lns to paragraphs of (read file srcFile as «class utf8»)
repeat with ln in lns
do shell script "npm lighthouse --throttling-method simulate --verbose --view --emulated-form-factor mobile --output-path "/Users/user/Documents/Lighthouse/LighthouseReports.csv" https://www.aphrodites.com/collections/bestsellers-home/products/tree-of-life-heart-edition-charm-bracelet-with-real-austrian-crystals"
end repeat
Some help would be fantastic, thank you!
The following will probably get you a bit closer. At least, it's valid AppleScript for what I think you're trying to do (never having used lighthouse before). I'm assuming that you want that given URL in the do shell script to be the URL stored in each line of the file, right?
The quoted form of command makes sure that paths are properly quoted for unix, saving you some headaches. I've used the full path to npm, because do shell script doesn't import your interactive shell PATH variable and won't find that utility. if you ever want to know wether do shell script will be able to find a utility, run do shell script "which utilityname"; if the command errors out, use the full path.
set srcFolder to "/Users/user/Documents/Lighthouse/"
set srcFile to srcFolder & "urls.txt"
set outputPath to quoted form of (srcFolder & "LighthouseReports.csv")
set lns to paragraphs of (read file srcFile as «class utf8»)
repeat with ln in lns
do shell script "/usr/local/bin/npm lighthouse --throttling-method simulate --verbose --view --emulated-form-factor mobile --output-path " & outputPath & " " & quoted form of ln
end repeat
I have run into some installation problems with the new Sierra update.
I want to run a script that checks the version number and deletes a certain .mpkg file based on the version number because I am having a lot of customers running the wrong installation which is causing a lot of issues. I have tried multiple versions of this code and nothing seems to be working. My result in Applescript console is: "".
Any help would be greatly appreciated.
tell application "Finder"
set os_version to do shell script "sw_vers -productVersion"
if ((os_version as string) is equal to "10.12") then
do shell script (" rm -rf \"Step 1 Installer.mpkg\" ")
else
do shell script (" rm -rf \"Step 1 Installer (SIERRA ONLY).mpkg\" ")
end if
end tell
The problem is that when you run the rm command, you don't specify the directory the .mpkg file is in. It does not automatically look in the same directory the script is in. Instead, it looks in whatever the script's working directory is, which seems to be / (i.e. the top level of the system volume). You can use path to me to get the script's path, then you need to convert that to a POSIX path in quoted form to use in the shell, then get the parent directory name... Here's what I came up with:
set scriptPath to POSIX path of ((path to me) as string)
do shell script "rm -Rf \"$(dirname " & (quoted form of POSIX path of (scriptFile)) & ")/Step 1 Installer.mpkg\""
(and a similar variant for the other installer)
Warning: *I have not fully tested this, and it contains an rm -Rf command. Thus, if something goes wrong it could go very very wrong. Test well, on a system that you don't care about.
I am attempting to make a script to mount a drive. As this is something I do frequently. However, I am not familiar with Applescript. Thus far I have not been able to find how to pass a query into a command?
If you wouldn't mind showing me that would be awesome! A little explanation would also be great as I do want to understand what it does.
(choose from list {"disk0s2", "disk2s1", "disk2s2", "disk3s1", "disk3s2", "disk4s1", "disk4s2"} with prompt "What disk are you trying to mount?")
set disk to result as text
repeat 5 times
do shell script "diskutil mount readOnly /dev/disk3s1" with administrator privileges
end repeat
you must replace part of the do shell script command with your variable and concat all with & :
do shell script "diskutil mount readOnly /dev/" & disk" with administrator privileges
Do you need to use Applescript? This is trivial with bash. If you make a script for each disk, like this:
#!/bin/sh
diskutil mount readOnly /dev/disk2s1
You can make the script double-clickable by right-clicking, Open With, then Other... and selecting Terminal. (You may need to select All Applications when browsing for the Terminal application).
set theAddresses to {"Address, Name, Counter" & return & "A#b.com, A, 1"}
set theFile to (path to desktop folder as text) & "test.csv"
do shell script "echo " & quoted form of (theAddresses as text) & " > " & quoted form of theFile
delay 1
do shell script "open " & quoted form of theFile
I get permissions error:
error "sh: Alwnick:Users:aleith1:Desktop:test.csv: Permission denied"
I tried to replicate this as a line command in bash terminal but I can't write string literal into a file with the " > " command. Nor can I find a manual for ">" fo syntax for literals. Yet echo seems to use a literal okay, despite the permissions issue.
Where should I change the permissions, in the shell script or manually in OSX Finder or in bash? What permissions should the Desktop Folder have? I tried to invoke "sudo echo…" but no gain.
You are trying to write to
Alwnick:Users:aleith...blah...blah...something
That is the Applescript version of the filename, and it is unintelligible to the shell and Unix command line utilities - basically it uses colons in places of slashes.
You need to use the "POSIX form" of the path, see here