Automator apple operation not permitted while trying to access volumes - macos

I'm trying to write an Automator that outputs a .csv that represents all the directories and subdirectories with relative disk usage in an external drive, the problem is I only get "operation not permitted" even if I'm under sudo.
here is my appleScript:
on run {input, parameters}
set volumename to first item of input
set filename to second item of input
do shell script "sudo du -h /Volumes/" & volumename & "/ > " & quoted form of (POSIX path of (path to desktop)) & "" & filename & ".csv" with administrator privileges
end run
I've already tried to give full disk permissions to my test.app but doesn't work.

Related

Can't write using AppleScript/ShellScript to a file in the main library/Application Support folder

I'm trying to write some text into an xml file situated in a subfolder of the main library/Application Support folder using a shell script
do shell script "echo '" & theText & "' > " & thePath.
Without a password, I get a "sh: /Library/Application: Permission denied" which is perfectly logical.
Adding user name and password, as shown in the code below, I no longer get any error, but no text is written to the file.
If I put a wrong user name or password, it gives me "The user name and password were incorrect", which shows that the password is indeed being taken into account.
Am I trying to do something impossible, or is my code missing something ?
set thePath to POSIX path of ("/Library/Application Support/MyApp/Stuff/test.xml" as text)
set theText to "<ProductSpecific><Visibility type=\"Number\">3</Visibility></ProductSpecific>"
set theScript to "echo '" & theText & "' > " & thePath
do shell script theScript user name "myshortusername" password "mypassword" with administrator privileges
I should get theText written to /Library/Application Support/MyApp/Stuff/test.xml
but nothing is written although I don't get an error message either ! Oh, and if I move the file to the desktop and change the path, it all works fine !!
The reason of the error is the space character in Application Support. You have to escape the entire path.
It's highly recommended to use always quoted form of
set theScript to "echo " & quoted form of theText & " > " & quoted form of thePath
Side note:
The string path is already a POSIX path (and it's already text) so you can omit both
set thePath to "/Library/Application Support/MyApp/Stuff/test.xml"

Run a command line tools with arguments in AppleScript

I did the following and it works fine, I added my Command Line Tool to the Resource Folder of my project and did the following below:
set myPath to POSIX path of (path to resource "MyComandLineTool")
do shell script "cp " & quoted form of POSIX path of myPath & space & "/private/tmp" with administrator privileges
set cmd to "/private/tmp/myPath -r"
do shell script " " & cmd with administrator privileges
I wanted to know how not to pass it to /private/tmp, just run it from within the Resources folder of my project.
It's quite similar, just omit the copying part
set myPath to quoted form of (POSIX path of (path to resource "MyComandLineTool"))
do shell script myPath & " -r" with administrator privileges
Note: quoted form of is always good practice as the path could contain space characters

Applescript: Need Help Resolving - Can't Get every file of said folder?

With the help of some users on this form I was able to create this script below which lets you choose a folder then choose which folders within the folder you want to rsync into a OneDrive Backup Folder.
Basically were migrating from a on site network storage to OneDrive for Business and want to create a script that as easy as possible for our users.
The issue I have is I dont want the users to be able to choose the original folder, I want to set the variable up front.
In the script I use:
set theFolder to (choose folder with prompt "Please Choose The Root of Your H Drive Or The Folder That Looks Like: " & userName & "$")
If I use:
set theFolder to "/Volumes/MYERSMI5$/"
I get "Can't Get every file of said folder" error message.
How Do I set the theFolder for this script ahead of time instead of asking the user to pick the folder?
set OuserName to do shell script "whoami"
set userName to do shell script "echo " & OuserName & " | tr a-z A-Z"
tell application "Finder"
if not (disk userName exists) then
mount volume "SMB Server/" & userName & "$"
end if
delay 2
set theDialogText to "
- Mac H-Drive Migration Tool -
This Application Will Migrate a Copy of Your H Drive Data
to your OneDrive for Buisness Folder Locally on Your Mac
Migration Backup Location:
/Users/" & OuserName & "/OneDrive Folder/H-Drive Migration Backup
** Important **
In the Next Window Please Choose
The Root Folder of Your H Drive
The Drive Label Should Look Like: " & userName & "$"
display dialog theDialogText
set theFolder to (choose folder with prompt "Please Choose The Root of Your H Drive Or The Folder That Looks Like: " & userName & "$")
do shell script "mkdir -p ~/'OneDrive Folder'/'H-Drive Migration Backup'"
set HDriveBackupFolder to ((path to home folder as text) & "OneDrive Folder:H-Drive Migration Backup")
set AppName to "OneDrive.app"
tell application "Finder" to set Answer_ to exists application file ((path to applications folder as string) & AppName)
if Answer_ is false then
beep
beep
beep
beep
beep
end if
delay 1.5
tell application "Finder"
activate
set theFolderNames to name of folders of theFolder
set theChosenNames to (choose from list theFolderNames with prompt "Choose Which Folders to Backup, Please Hold Down The ⌘ Key To Choose Multiple Folders " with multiple selections allowed)
if (theChosenNames is false) then return
set HDriveBackupFolder to ((path to home folder as text) & "OneDrive Folder:H-Drive Migration Backup")
end tell
repeat with thisName in theChosenNames
tell application "Terminal"
do script ("rsync -avpz --delete " & (quoted form of POSIX path of ((theFolder as text) & thisName)) & space & (quoted form of POSIX path of HDriveBackupFolder)
end tell
end repeat
end tell
You asked:
"How Do I set the theFolder for this script ahead of time instead of asking the user to pick the folder?"
Also, just prior to that, you said:
If I use:
set theFolder to "/Volumes/MYERSMI5$/"
I get "Can't Get every file of said folder" error message.
First, lets address setting the value of the theFolder variable. It should be as follows:
set theFolder to POSIX file "/Volumes/MYERSMI5$/"
I assume the error is being thrown at the following point in the code:
tell application "Finder"
activate
set theFolderNames to name of folders of theFolder
Change set theFolderNames to name of folders of theFolder to:
set theFolderNames to name of folders of container theFolder
To test this, I mounted a volume named MYERSMI5$ at /Volumes/ and created some folders within /Volumes/MYERSMI5$/. Then running the following code:
set theFolder to POSIX file "/Volumes/MYERSMI5$/"
tell application "Finder"
activate
set theFolderNames to name of folders of container theFolder
set theChosenNames to (choose from list theFolderNames with prompt "Choose Which Folders to Backup, Please Hold Down The ⌘ Key To Choose Multiple Folders " with multiple selections allowed)
if (theChosenNames is false) then return
end tell
It produced a list box, containing the names of the folders I created at that location, to choose from.
I did not try to run the entire block of code you included in your question, so if you're having other issues, you'll need to follow up after making the changes mentioned in my answer. There are good reasons why questions involving debugging code should conform to How to create a Minimal, Complete,and Verifiable example.
You may also want to review the Variables and Properties section in the AppleScript Language Guide.

Why this permissions denied issue with a shell script inside an Applescript writing to my desktop folder?

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

Applescript: Finder got an error: AppleEvent handler failed

I have this script set up or rename my torrent folders, however it keeps giving me that weird finder error. It works when I don't read it from a text file, but I need that so that I don't need to select the folder all the time: Please help:
set read_folder to read (POSIX path of "/.torrentcleanup-prefs.txt") as text
set autofolder to (POSIX file read_folder)
set folderlist to ("")
tell application "Finder" to set folderlist to (get name of folders of folder autofolder)
repeat with i in folderlist
set dfilepath to (POSIX path of ((autofolder & i) as text))
set dfoldername to quoted form of POSIX path of dfilepath
set dfolder to i
set dmovie to quoted form of (text 1 thru ((length of dfolder) - 7) of dfolder as text)
try
do shell script "cd " & dfoldername & "; mv ./*.mkv ../" & dmovie & ".mkv"
end try
try
do shell script "cd " & dfoldername & "; mv ./*.mp4 ../" & dmovie & ".mp4"
end try
do shell script "rm -r " & dfoldername & ""
end repeat
By the Way, the contents of "/.torrentcleanup-prefs.txt" is:
/Users/student/Desktop/FIX ME NOW/Test Folder/
I didn't look at your whole script but you're reading the file wrong in the first line. You need to do it like this. So replace your first line with these two...
set myFile to "/.torrentcleanup-prefs.txt"
set read_folder to read (POSIX file myFile)

Resources