XYMer's Home away from Home

When http://bbs.xlr8yourmac.com is down (i.e. always)
It is currently Fri May 26, 2017 8:53 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 13 posts ] 
Author Message
PostPosted: Tue Dec 06, 2016 10:51 am 
Offline

Joined: Thu May 15, 2008 8:21 pm
Posts: 1058
Location: Burblandia
This a little silly but for some reason (I don't care to resolve) my El Capitan install loses audio on sleep. I have tried many solutions but have decided that the easiest and least potentially harmful solution is just to unload and reload the AppleHDA kext (modified). This works fine.
Code:
sudo kextunload /System/Library/Extensions/AppleHDA.kext
sudo kextload /System/Library/Extensions/AppleHDA.kext

What I am asking for is help creating a clickable file I can leave on desktop or dock that will make those commands happen without my having to open terminal, copying the commands, pressing enter and closing terminal. I tried writing an Applescript but I just have too little skill in this arena. I think it is probably a simple task though for someone with the knowledge. Searching was not productive.


Top
 Profile  
 
PostPosted: Tue Dec 06, 2016 11:51 am 
Offline
User avatar

Joined: Thu May 15, 2008 8:13 pm
Posts: 9247
Location: Caught between the moon and NYC
You could do it with a shell script, although you would have to type your password in the Terminal window that opens as part of the first sudo.

Once the dust settles I'll whip one up. On a PC right now.

An Applescript would be more elegant and would present you with the typical privilege escalation dialog box (that you see when installing software, typically a window with your full name as the username and an empty password field you type into to authorize the action), but a shell script would be double-clickable and work.


Top
 Profile  
 
PostPosted: Tue Dec 06, 2016 12:24 pm 
Offline

Joined: Thu May 15, 2008 8:21 pm
Posts: 1058
Location: Burblandia
MonkeyBoy wrote:
You could do it with a shell script, although you would have to type your password in the Terminal window that opens as part of the first sudo.

Once the dust settles I'll whip one up. On a PC right now.

An Applescript would be more elegant and would present you with the typical privilege escalation dialog box (that you see when installing software, typically a window with your full name as the username and an empty password field you type into to authorize the action), but a shell script would be double-clickable and work.

I like the Applescript idea as it will provide a familiar user dialogue box. I tried to use ScriptEditor but can't seem to get started properly. Got this far:
Code:
tell application "Terminal"
   
   activate
   do shell script "sudo kextunload /System/Library/Extensions/AppleHDA.kext"
   do shell script "sudo kextload /System/Library/Extensions/AppleHDA.kext"
end tell
But got the error:
Code:
error "Terminal got an error: sudo: no tty present and no askpass program specified" number 1

I'm sure there are several ways to accomplish this. One would be awesome!


Top
 Profile  
 
PostPosted: Tue Dec 06, 2016 12:59 pm 
Offline
Benevolent Dictator
User avatar

Joined: Mon Apr 21, 2008 2:03 am
Posts: 14420
Quote:
sudo: no tty present and no askpass program specified

I was able to work around it (in a sense problem solved) by using:

do shell script "mv " & posixPath1 & "* " & posixPath2 user name admin_name password admin_password with administrator privileges

Even though this works fine, I am confused as to why the sudo style stopped working.

http://macscripter.net/viewtopic.php?id=31230


Top
 Profile  
 
PostPosted: Tue Dec 06, 2016 2:06 pm 
Offline
User avatar

Joined: Thu May 15, 2008 8:13 pm
Posts: 9247
Location: Caught between the moon and NYC
The shell script is easier to create, since it's just a text file with the contents of what you wrote out, a couple minor things need to be changed on the file itself and voila, your text file becomes a shell script.

As far as an Applescript goes, this is what I've discovered:
do shell script "kextunload /System/Library/Extensions/AppleHDA.kext" with administrator privileges
do shell script "kextload /System/Library/Extensions/AppleHDA.kext" with administrator privileges

Basically you can't issue a sudo in applescript, you have to use the with administrator privileges flag so applescript itself escalates its privilege then issues the command as itself.

Don't get me wrong, I like Applescript, I use it all the time for automating projects that others have to use regularly, but for stuff like this that's terminal-based a shell script is just easier.


Top
 Profile  
 
PostPosted: Tue Dec 06, 2016 2:13 pm 
Offline
User avatar

Joined: Thu May 15, 2008 8:13 pm
Posts: 9247
Location: Caught between the moon and NYC
It took me longer to dig up an applescript example (which I basically knew but couldn't remember the syntax of) than create this. I tend to obey the KISS philosophy for stuff I use.

The script is literally just the following text, with a chmod +x on the file to make it executable, then make it open in Terminal, then zip it up so it remembers those two changes for you.
Code:
#!/bin/bash
sudo kextunload /System/Library/Extensions/AppleHDA.kext
sudo kextload /System/Library/Extensions/AppleHDA.kext


Attachments:
unloadload.sh.zip [668 Bytes]
Downloaded 25 times
Top
 Profile  
 
PostPosted: Tue Dec 06, 2016 4:30 pm 
Offline

Joined: Thu May 15, 2008 8:21 pm
Posts: 1058
Location: Burblandia
MonkeyBoy wrote:
It took me longer to dig up an applescript example (which I basically knew but couldn't remember the syntax of) than create this. I tend to obey the KISS philosophy for stuff I use.

The script is literally just the following text, with a chmod +x on the file to make it executable, then make it open in Terminal, then zip it up so it remembers those two changes for you.
Code:
#!/bin/bash
sudo kextunload /System/Library/Extensions/AppleHDA.kext
sudo kextload /System/Library/Extensions/AppleHDA.kext

Thanks MB! If I understand correctly I can use the file as it is ...in other words I do Not need to do the "chmod +x on the file to make it executable". I will let you know how it works as soon as my audio gets bumped out again. Looking at the file contents shows just exactly what you indicated, and I am assuming that the "chmod +x on the file to make it executable" is what you pointed at the file to make it work (I just sound like a silly talking codeless fool).


Top
 Profile  
 
PostPosted: Tue Dec 06, 2016 4:55 pm 
Offline
User avatar

Joined: Thu May 15, 2008 8:13 pm
Posts: 9247
Location: Caught between the moon and NYC
Sorry, yeah, my instructions were for if you wanted to create your own shell script.

Before zipping it up I ran chmod on the file and changed it to open with Terminal, so no need to do that on the file provided. Files compressed through Finder preserve the owner:group, privilege flags, and metadata (application creator), which is why its handy to share files that way.

By default .sh files want to open up in XCode on my system, without XCode it might default to Terminal. Its been so long since I haven't had some form of XCode installed I forget what it does.


Top
 Profile  
 
PostPosted: Tue Dec 06, 2016 7:56 pm 
Offline

Joined: Thu May 15, 2008 8:21 pm
Posts: 1058
Location: Burblandia
MonkeyBoy wrote:
Sorry, yeah, my instructions were for if you wanted to create your own shell script.

Before zipping it up I ran chmod on the file and changed it to open with Terminal, so no need to do that on the file provided. Files compressed through Finder preserve the owner:group, privilege flags, and metadata (application creator), which is why its handy to share files that way.

By default .sh files want to open up in XCode on my system, without XCode it might default to Terminal. Its been so long since I haven't had some form of XCode installed I forget what it does.

Thanks for the explanation MB. Very clear now.
======update======
Audio disappeared this morning but the script worked perfectly. Very easy to use just a single PW request and then quit terminal. Thanks again MB.


Top
 Profile  
 
PostPosted: Thu Dec 08, 2016 10:07 am 
Offline

Joined: Sat Sep 27, 2008 6:28 pm
Posts: 188
Working applescript with admin privileges, modify as needed.

Code:
(*

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.
   
   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 unixDesktopPath to POSIX path of "/System/Library/User Template/"
   log "unixDesktopPath = " & unixDesktopPath
   
   set quotedUnixDesktopPath to quoted form of unixDesktopPath
   log "quoted form is " & quotedUnixDesktopPath
   
   try
      set fromUnix to do shell script "sudo ls -l  " & quotedUnixDesktopPath with administrator privileges
      display dialog "ls -l of " & quotedUnixDesktopPath & return & fromUnix
   on error errMsg
      log "ls -l error..." & errMsg
   end try
   
end run



Top
 Profile  
 
PostPosted: Thu Dec 08, 2016 10:37 am 
Offline

Joined: Thu May 15, 2008 8:21 pm
Posts: 1058
Location: Burblandia
rccharles wrote:
Working applescript with admin privileges, modify as needed.

Code:
(*

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.
   
   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 unixDesktopPath to POSIX path of "/System/Library/User Template/"
   log "unixDesktopPath = " & unixDesktopPath
   
   set quotedUnixDesktopPath to quoted form of unixDesktopPath
   log "quoted form is " & quotedUnixDesktopPath
   
   try
      set fromUnix to do shell script "sudo ls -l  " & quotedUnixDesktopPath with administrator privileges
      display dialog "ls -l of " & quotedUnixDesktopPath & return & fromUnix
   on error errMsg
      log "ls -l error..." & errMsg
   end try
   
end run


Thanks rccharles. Candidly, I don't know what to do with this. I'm really a novice on anything related to code, even apple-scripting.


Top
 Profile  
 
PostPosted: Tue Dec 13, 2016 11:49 am 
Offline
User avatar

Joined: Thu May 15, 2008 8:07 pm
Posts: 2384
Location: Inside Flatus Maximus
Thankfully I don't need to enter my password, since I changed my sudoers file loooooooong ago to allow my primary account full access without the password (primarily to aid in launching D3 via Terminal after the script running Purge).

_________________
Official Mac Tech Support Forum Cookie™ (Mint Chocolate Chip)
Guaranteed tasty; Potentially volatile when dipped in WWIII Forum Syrup®
Caution: This cookie bites back.


Top
 Profile  
 
PostPosted: Tue Dec 13, 2016 5:36 pm 
Offline
Master

Joined: Sun Apr 20, 2008 5:24 am
Posts: 9609
Location: North of the State of Jefferson
That's living dangerously. It's comparable to just logging in as root.

- Anonymous


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group