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.
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.
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
I searched here and on the net, but didn't really find what I was looking for (I'm still a newbie in bash programming).
I want to start a small bash-script via automator-app.
My bash-script and the automator-app should be in the same directory. The app should just start the script (without parameters). (The script manipulates files in the directory where it is located).
I don't want to copy the script somewhere and have the app somewhere else, both should be in the same directory.
However, Automator does not find the bash script.
How can I specify the path correctly?
In Automator I said:
Run-Shell-Script
myshellscript.sh
myshellscript.sh -> is not found. And I don't want a fixed path. The path of the app (and script) should be my working path.
Can someone help me? That would be great.
Solution found:
property myPath : ""
set myPath to POSIX path of ((path to me as text) & "::") as text
do shell script myPath & "myscript.sh " & myPath
How can I make the Win-Bash prompt always print the current folder?
I want it to display something like:
MyPC /Data/MyFiles # _
(assuming I'm in the folder /Data/MyFiles)
whereas right now, it always displays
bash$ _
no matter what folder I'm in.
You should be able to set your prompt like this:
PS1="\w\$ "
and put that in your ~/.bashrc file.
Win-Bash apparently uses a very old version of Bash, so it's going to be somewhat limited.
In Applescript how do I read a website, and output the text as a variable?
I want to read the "Latest" number from here, then use that number to download the latest version of Chromium from here.
Not a direct Applescript way, but this works well.
Create a shell script to do the download, say chrome-download.sh in your $HOME/bin directory:
#!/bin/sh
BUILD=`curl http://build.chromium.org/buildbot/snapshots/sub-rel-mac/LATEST`
echo "Downloading build "$BUILD
curl http://build.chromium.org/buildbot/snapshots/sub-rel-mac/$BUILD/chrome-mac.zip -o $HOME/chrome-mac-$BUILD.zip
Run it from Applescript (or Automator) in one line:
do shell script "/bin/bash /Users/<your username>/bin/chrome-download.sh"
The downloaded file lands in your $HOME directory. The bash script will work from Linux or Cygwin, as well. Of course, you could just run the bash script directly, too.
As Glenn said it's much easier to just use a shell script and possibly wrap it in AppleScript by using do shell script. Another alternative if you want a GUI of sorts to the script is to look at a program called Platypus.
Lastly if you're looking for a sample script that already does this I made one when Chromium Mac was announced a couple days back: http://chealion.ca/2009/05/chromium-build-bot-updater-script/ (Source on GitHub).
A more AppleScript-native way would be:
set filePath to (path to temporary items folder as string) & "file.html"
tell application "URL Access Scripting"
download "http://www.apple.com/sitemap/" to file filePath replacing yes
end tell
--read file into a variable
try
open for access (file filePath)
set fileData to (read file filePath)
close access (file filePath)
on error errMsg number errNum
try
close access file filePath
end try
error errMsg number errNum
end try
You can find more information in the URL Access Scripting and StandardAdditions dictionaries. (File > Open Dictionary)