Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript bizarre error

do shell script "cd " & selectedPath & "; say -v \"Samantha\" \"" & typeText & "\"" & finalName & " -o \"" & fileName & "\"" with administrator privileges


Here's the weird thing... if I remove "with administrator privileges," the script throws an error message "cd: no such file or directory."


If I remove the variable selectedPath and type out the path manually, it works without admin access. However, selectedPath and the path I typed out are exactly the same (I added a display dialog command which prints out selectedPath to make sure it was formatted correctly).


1) If it were a permissions issue, I would think I would get an error related to permissions. Anyone know why I got that error?

2) Why are admin privileges required just to change the directory, and why is it different for a variable vs. hard-coded text?

 MacBook Air-OTHER, OS X Mavericks (10.9.5), 13 in, Mid 2013, Core i5, 256GB

Posted on Apr 17, 2015 11:37 AM

Reply
Question marked as Best reply

Posted on Apr 17, 2015 8:54 PM

I like to print out what is passed to unix. So, I do:


set unix_input to "cd " & selectedPath & "; say -v \"Samantha\" \"" & typeText & "\"" & finalName & " -o \"" & fileName & "\""

log "unix_input=" & unix_input

do shell script unix_input with administrator privileges




I cannot make much sense of this. It seems you have a cd then a say command. Don't know why this needs admin.


and you do have the problem of funnie characters in the filename. Use "quoted form". Manual quoting may not be enough. Here is the way:


(*


It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on. Here is an example.



Author: rccharles


For testing, run in the Script Editor.

1) Click on the Event Log tab to see the output from the log statement

2) Click on Run


For running shell commands see:

http://developer.apple.com/mac/library/technotes/tn2002/tn2065.html




*)



on run

-- Write a message into the event log.

log " --- Starting on " & ((current date) as string) & " --- "

-- debug lines

set desktopPath to (path to desktop) as string

log "desktopPath = " & desktopPath


set unixDesktopPath to POSIX path of desktopPath

log "unixDesktopPath = " & unixDesktopPath


set quotedUnixDesktopPath to quoted form of unixDesktopPath

log "quoted form is " & quotedUnixDesktopPath


try

set fromUnix to do shell script "ls -l " & quotedUnixDesktopPath

display dialog "ls -l of " & quotedUnixDesktopPath & return & fromUnix

on error errMsg

log "ls -l error..." & errMsg

end try


end run

21 replies

Apr 18, 2015 2:58 PM in response to rccharles

Yep, it's just a kludge thrown together as a test, on that subject have you seen what your example logs…

error "The variable homepPath is not defined." number -2753 from "homepPath"

😝



The Apple tech note on do shell script note uses '~' too…

do shell script "cd ~/Documents; ls" -- result: "Welcome.txt"

https://developer.apple.com/library/mac/technotes/tn2065/_index.html

They don't seem to say why tilde is bad.


We all have nits, lets leave them alone 🙂

Apr 18, 2015 3:15 PM in response to rccharles

The thing is though, if I hard-code that file path into the script, it works. It's only if I have a variable representing the path that it complains.


Anyway, I'll try to avoid using the tilde. But how would I concatenate the file path? Would I use:

  1. set homePath to (path to home folder) as string
  2. log "homePath = " & homepPath

  3. set unixhomepPath to POSIX path of homePath

    \

  4. log "unixhomePath = " & unixhomePath

...and then say do shell script "cd " & unixhomePath & "/Desktop/VR_Ringtones" ?

Apr 18, 2015 3:33 PM in response to stevejobsfan0123

That's the way, but I suggest including a quoted form. -- parts untested --


set desktopPath to (path to desktop) as string

log "desktopPath = " & desktopPath


set unixDesktopPath to POSIX path of desktopPath

log "unixDesktopPath = " & unixDesktopPath


-- Who knows if you need the leading slash or not. ( multiple /'s actually work )

set unixDesktopPath to unixDesktopPath & "/VR_Ringtones"


set quotedUnixDesktopPath to quoted form of unixDesktopPath

log "quoted form is " & quotedUnixDesktopPath

Apr 18, 2015 11:17 PM in response to stevejobsfan0123

Why do you not show the complete code to demonstrate the issue you're observing?


The code snippet you provide in the original post is not complete and cannot be tested. Please provide something like the following script so that readers can test it clearly.



-- code 1 (failing without administrator privileges ?) set selectedPath to "~/Desktop" set typeText to "Hello, " set finalName to "World" set fileName to "hello.aac" do shell script "cd " & selectedPath & "; say -v \"Samantha\" \"" & typeText & "\"" & finalName & " -o \"" & fileName & "\"" --with administrator privileges -- code 2 (working without administrator privileges ?) do shell script "cd ~/Desktop; say -v \"Samantha\" \"Hello, \"World -o \"hello.aac\""



Regards,

H

AppleScript bizarre error

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.