The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   OS X Developer (http://hintsforums.macworld.com/forumdisplay.php?f=27)
-   -   Set Location Via Applescript according to time of day? (http://hintsforums.macworld.com/showthread.php?t=34072)

Caius 01-27-2005 08:20 PM

Set Location Via Applescript according to time of day?
 
Hi,

I use my laptop for college, and annoyingly we don't have an airport connection at college, so airport is off, and also there are different e'net settings, etc, etc.

Basically, I have a Home profile, and a College profile. And rather than switch between the two manually all the time (i forget ;)), I decided today that I would use it to start to develop my interest in Applescript further.

So far I have the time limiting set correctly for what i want to do. I'm calling SEC Helper as it imitates the mute key on the keyboard, and so when its restored at the end of the college day, its the same volume as the beginning (providing I haven't manually adjusted it of course).

Here is the code I have so far:
Code:

set c to (time of (current date)) / hours
tell application "SEC Helper"
        if c < 9 then
                tell application "SEC Helper" to simulate keyboard special key volume up
        else if c ? 9 and c < 15 then
                tell application "SEC Helper" to simulate keyboard special key toggle mute
        else if c ? 15 then
                tell application "SEC Helper" to simulate keyboard special key volume up
        end if
end tell

Also as a side note, would it be possible to write the system volume level to a file somewhere when it was muted (a .plist in the users preference folder perhaps?) so it knows to set it to that exact level when it unmutes?

Cheers,
Nemo

davewalcott 01-27-2005 10:05 PM

Quote:

Originally Posted by iNemo
would it be possible to write the system volume level to a file somewhere when it was muted (a .plist in the users preference folder perhaps?) so it knows to set it to that exact level when it unmutes?

Sure, just create 2 bash shell scripts (i.e. "read_volume.sh" and "write_volume.sh") to read and write the volume to some text file, and then call each one from your AppleScript using the "do shell script" AppleScript command, like:

do shell script "/bin/sh read_volume.sh"

Caius 01-28-2005 04:49 AM

Thanks. :)

The volume control was just an added idea, the main reason was wanting to change the location without having to GUI script to use the apple menu -> location.

Was hoping that there was a way to script this so it could run in the background when activated by something along the lines of an app I saw recently that runs a script on the machine waking up.

bramley 01-28-2005 06:37 AM

Why script location changes when you can make it automatic?

Open System Preferences -> Network -> Show "Network Port Configuration" and order the network ports in the order you want them. i.e put Airport first and then (I assume) Ethernet. Activate both. Set "Location" to automatic. Job done. If there's no Airport then the machine will try and use Ethernet.

Caius 01-28-2005 08:37 AM

Doesn't turn airport off though so it still uses more battery power. Thats the main reason I want to automate the location switching. Plus I sometimes have to use a different ethernet connection at home compared to what I have the settings set to college, so I do actually need the two different locations.

Looks like I will have to just have the volume automated, and have to continue o remember to change the location. I suppose I could program in a dialog box that reminds me to change the location to the appropriate one according to the time.

davewalcott 01-28-2005 01:44 PM

Try the "scselect" terminal command to script location changes. From AppleScript, it would look like:

do shell script scselect <location_name>

Caius 01-28-2005 01:54 PM

My god that is so simple. Just what I was looking for. Thanks very much dave.

Afraid I need the commands for the volume read/write documenting in a bit more detail, but for now its off to the applescript site to see how to do that :)

styrafome 01-28-2005 01:59 PM

Wow, maybe you could get together with these guys and make some money:
http://homepage.mac.com/locationmanager/

bramley 01-28-2005 02:32 PM

Quote:

Originally Posted by davewalcott
Try the "scselect" terminal command to script location changes. From AppleScript, it would look like:

do shell script scselect <location_name>

Good call. Didn't know this!

@iNemo. Unless there is some reason why you must use "SEC Helper", note that AS already has commands to change the volume. See the Standard Additions scripting dictionary under 'Miscellaneous' for details.

Caius 01-28-2005 07:04 PM

I don't have any reason to use SEC Helper, except that was the command I found out and that started me thinking about such a script.

I will indeed look into the AS control of the volume as I'm sure its a bit more in depth than simply imitating the key press.

PS. This is the script so far in case anyone is interested:

Code:

-- Location changing script according to time.
-- Compiled by Nemo Pascal

set c to (time of (current date)) / hours
tell application "SEC Helper"
        if c < 9 then
                tell application "SEC Helper" to simulate keyboard special key volume up
                do shell script "scselect Home"
                activate
                display dialog "Location changed to Home" buttons {"Ok"} default button "Ok" giving up after 10
               
        else if c > 9 and c < 15 then
                tell application "SEC Helper" to simulate keyboard special key toggle mute
                do shell script "scselect College"
                activate
                display dialog "Location changed to Home" buttons {"Ok"} default button "Ok" giving up after 10
               
        else if c ? 15 then
                tell application "SEC Helper" to simulate keyboard special key volume up
                do shell script "scselect Home"
                activate
                display dialog "Location changed to Home" buttons {"Ok"} default button "Ok" giving up after 10
        end if
end tell

Now I have to go read the applescript dictionary to implement AS changing the volume. Also have to flag up a dialog box asking which location to set to, defaulting to the default one according to the time, with a timeout of a few seconds.. I have big plans for this one script :)

yeidel 01-28-2005 07:46 PM

Getting Location Script a Little Closer
 
Quote:

Originally Posted by davewalcott
Try the "scselect" terminal command to script location changes. From AppleScript, it would look like:

do shell script scselect <location_name>

It didn't work when typed into Script Editor as shown. I futzed around and came up with
Code:

tell application "Finder"
        do shell script "scselect ALocationName"
end tell

Just a little fancier (a reporting dialog box):
Code:

tell application "Finder"
        do shell script "scselect AtCTLT"
end tell
set loc to "Location set to: " & the last word of the result
display dialog loc buttons {"OK"} default button 1


Caius 01-29-2005 12:39 AM

Quote:

Originally Posted by bramley
Good call. Didn't know this!

@iNemo. Unless there is some reason why you must use "SEC Helper", note that AS already has commands to change the volume. See the Standard Additions scripting dictionary under 'Miscellaneous' for details.

I found the command

Code:

get volume settings
which when run returns

Code:

{output volume:100, input volume:100, alert volume:100, output muted:false}
Now I just need to find a way to parse the correct data from the result. But not now. Have to go and work now :mad:

robJ 02-02-2005 11:32 PM

Quote:

Originally Posted by iNemo
I found the command

Code:

get volume settings
which when run returns

Code:

{output volume:100, input volume:100, alert volume:100, output muted:false}
Now I just need to find a way to parse the correct data from the result. But not now. Have to go and work now :mad:

Does this work?

Code:

tell (get volume settings)
        set alert_vol to alert volume
        set input_vol to input volume
        set output_muted to output muted
        set output_vol to output volume
end tell

-- Rob

Caius 02-03-2005 02:23 AM

Looks like it should. Will test it at college later and let you know tonight. (I refuse to use the pc's at college to access the internet until the techies put the powerbook on the intranet :))

Sean Zhu 06-04-2006 09:52 PM

Appscrpt says: Where is SEC Helper?
 
Appscrpt says: Where is SEC Helper?

Sean Zhu 06-04-2006 09:55 PM

More
 
Also I cant save without telling Applescript.

Caius 06-04-2006 10:59 PM

Quote:

Originally Posted by Sean Zhu
Appscrpt says: Where is SEC Helper?

SEC Helper is the backend of Salling Clicker

I use it because it imitates keys easily, although you can replicate this in system events (I think, I know you can replicate keys, presume volume keys are there as well.)

You'd need to mess around with either getting system events to press the volume keys, or to use volume settings to change the system volume.

voldenuit 06-05-2006 03:45 AM

The best way to switch AP on and off would probably be to piggypack on configd whenever it detects the plugging/unplugging of an ethernet cable.
To handle several locations, you could use the MAC-address of the default gateway.

I wouldn't be surprised if there were already solutions existing based on that.

Caius 06-05-2006 12:11 PM

The way I've got it setup is that I have a location with airport and ethernet not configured. It turns airport off, and kills all network traffic.

I'm not always plugged into an ethernet cable when I leave the network, so disabling airport on leaving ethernet doesn't always work.

NB:
If you look at the date of the first post here, I actually started this last year sometime. When my HD crashed and burned I lost all the code, so I'd forgotten all about this. Now I've got time this summer I'll have to play with it again.

hayne 06-05-2006 12:47 PM

Quote:

Originally Posted by voldenuit
The best way to switch AP on and off would probably be to piggypack on configd whenever it detects the plugging/unplugging of an ethernet cable.
To handle several locations, you could use the MAC-address of the default gateway.

I wouldn't be surprised if there were already solutions existing based on that.

Indeed.
Have a look at these macosxhints articles for starters:
http://www.macosxhints.com/article.p...60122060330816
http://www.macosxhints.com/article.p...05010613401823
http://www.macosxhints.com/article.p...40923133938271

Sean Zhu 06-06-2006 08:31 PM

Salling Clicker costs how much?

Sean Zhu 06-06-2006 08:46 PM

Can I not use Salling Clicker?

Caius 06-06-2006 09:02 PM

Quote:

Originally Posted by Sean Zhu
Can I not use Salling Clicker?

If you buy it sure (it'll work with the demo as well I'd imagine though.)

It costs $23.95 according to the webpage http://salling.com/Clicker/mac/


All times are GMT -5. The time now is 05:41 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.