Previous 1 2 Next 16 Replies Latest reply: May 11, 2008 3:07 PM by V.K.
Glassix Level 1 Level 1 (0 points)
I've searched the forums and I've studied Keyboard shortcuts, but I still can't figure out if there's a way to assign a keyboard shortcut to simply switch between 2 users on my iMac. I'd love to make the F19 button work for this purpose. Anyone know how? A 3rd party app perhaps?

Thanks!

Intel 24" iMac, Mac OS X (10.5.2)
  • Terence Devlin Level 10 Level 10 (134,915 points)
    Glassix

    Welcome to the Apple Discussions.

    Check out Butler or LanchBar.

    Regards

    TD
  • nerowolfe Level 6 Level 6 (13,070 points)
    Glassix wrote:
    I've searched the forums and I've studied Keyboard shortcuts, but I still can't figure out if there's a way to assign a keyboard shortcut to simply switch between 2 users on my iMac. I'd love to make the F19 button work for this purpose. Anyone know how? A 3rd party app perhaps?

    Thanks!

    Welcome to the Apple boards:
    You could write a short script and assign it to F19.

    Message was edited by: nerowolfe
  • V.K. Level 9 Level 9 (56,110 points)
    as others have mentioned you need a 3rd party application launcher like Quicksilver, Butler,Launchbar, ikeys, Quickey.

    Plus you need a script that clicks the appropriate button.
    Something along the following lines:

    ----------------------
    tell application "System Events" to tell the front menu bar of process "SystemUIServer"
    tell (first menu bar item whose value of attributes contains "user")
    click
    tell (1st menu item of front menu whose name is "username")
    click
    end tell
    end tell
    end tell
    ------------

    paste it into Script Editor, substitute the name of the user you want to switch to for username, and save it as an application (or as a script). Then bind a shortcut to it using the app launcher you've installed.
  • Glassix Level 1 Level 1 (0 points)
    Thanks for the responses. I'm not new to Apple computers, but I'm certainly a stranger to scripting. If I'm able to write a short script to assign one of the keys to do my task would I even need a 3rd party application (like the examples given above)? I'm not entirely sure where one ends and the other starts in this case.

    And if I were to create a specific key to switch to a specific username, would I then do the same process for each user. Since there's only 2 of us I was hoping that one key would just switch back and forth, if possible.

    I haven't looked at it yet, so my next step is to study Applescript more closely to see if I can figure it out. Thanks V.K. for the detailed script. I'll use that as a guide, if I get to that point.
  • nerowolfe Level 6 Level 6 (13,070 points)
    If you create a script using Automator.app, you simply do what you normally do and the app "follows" your keystrokes and mousings. Can't be any simpler than that.
  • V.K. Level 9 Level 9 (56,110 points)
    Glassix wrote:
    Thanks for the responses. I'm not new to Apple computers, but I'm certainly a stranger to scripting. If I'm able to write a short script to assign one of the keys to do my task would I even need a 3rd party application (like the examples given above)? I'm not entirely sure where one ends and the other starts in this case.


    when you run the script it clicks on the appropriate item in the fast user switching menu. You can place it (or its alias) on the desktop and invoke it by double-clicking. No 3rd party tools required for that. However, if you want to use a keyboard shortcut to call it, you need to use one of those 3rd party app launchers to bind a shortcut to the script.



    And if I were to create a specific key to switch to a specific username, would I then do the same process for each user. Since there's only 2 of us I was hoping that one key would just switch back and forth, if possible.


    that's possible but you have to modify the script slightly. lets's say you have just two users in the fast user switching menu user1 and user2 in alphabetical order.

    then the script will be:

    -------------------

    set cur_user to do shell script "whoami"

    if cur_user is equal to "shortname-of-user1" then
    tell application "System Events" to tell the front menu bar of process "SystemUIServer"
    tell (first menu bar item whose value of attributes contains "user")
    click
    tell (2nd menu item of front menu)
    click
    end tell
    end tell
    end tell
    else

    tell application "System Events" to tell the front menu bar of process "SystemUIServer"
    tell (first menu bar item whose value of attributes contains "user")
    click
    tell (1st menu item of front menu)
    click
    end tell
    end tell
    end tell

    end if

    --------------

    Put the short name of user 1 in the above. This script will work regardless of which user is logged in. However, you have to create keyboard shortcuts for each user separately since they are not global.
  • Glassix Level 1 Level 1 (0 points)
    Hey V.K.,

    Before I saw your post I actually went to the Applescript website to read more about the application. While there I found some scripts ready to go, including an applet called "Account Switcher." It does what your helpful script would have done. After downloading it I was able to put the applet icon in the dock for easy clicking. *BTW, anyone out there looking to do the same thing, here is the link to the instruction page (with the download at the bottom):*

    http://www.apple.com/applescript/accountswitcher/index.html

    As you know, my goal was to create a keyboard shortcut since clicking in the dock isn't all that different than clicking up in the menu bar.

    So next, I downloaded and installed Quicksilver:

    http://www.blacktree.com/

    Nice little program. After figuring out how it works (not the easiest thing at first...and assigning hot key "triggers" took even more time) I was able to set up my F13 button to activate the "Account Switcher" applet. Success! Well, not total success since the program was unable to assign commands to the F16-F19 buttons (I wanted to use that perfect F19 corner button, darn it!) I'm not sure what's up with that, and a discussion search found others wondering the same thing.

    One last thing. I'll work with this set up for now, but I'm not entirely sure I like the lag time between switching users. When it's set up you actually watch the computer go through the motions: open the users drop down menu in the upper right corner, select other user, name and password screen appears, password is typed in, and THEN the "big box" rolls around to the other user. Amusing to watch, but in time the extra seconds might get old, so buyer (all this stuff was free, btw) beware.

    Still, it's cool that a work around exists. And I thank you for helping me explore the Applescript angle.

    Take care.
  • V.K. Level 9 Level 9 (56,110 points)
    I should have figured that someone made an app like this already.

    have fun with Apple script. It's really cool to play with.

    BTW, I checked out that apple made script in the first link you gave me. Does the script work for you as is? For some reason it doesn't for me. I had to open the package and modify the source script inside to make it work. The problem is that when the script looks for the menu item for fast user switching, it's programmed to look for the menu whose name is the same as my short login name when in fact, it should look for the one with my long login name. when I change the script in the application bundle to look for the long name instead, it works as it should.

    I'm curious about this. is it me or is the script buggy? I guess if your long name is the same as your short name then it shouldn't matter but that's not usually the case, is it?
  • Glassix Level 1 Level 1 (0 points)
    Oh yeah, the set up was a bit off in the "Account Switcher" applet. Because our user accounts appear as long name in the menu bar I had to change it to short name in the preferences first. But then when I typed the short name into the program it didn't accept it. That's when I noticed that even though the short name appears in the menu bar, the long name exists in the drop down menu. At that point the applet needs the drop down info instead. I think that's confusing enough that it should be mentioned in the instructions.
  • V.K. Level 9 Level 9 (56,110 points)
    Glassix wrote:
    Oh yeah, the set up was a bit off in the "Account Switcher" applet. Because our user accounts appear as long name in the menu bar I had to change it to short name in the preferences first.


    that's not really a good option. you shouldn't have to do that. Thanks for confirming that the script is a bit buggy. anyway, I just figured out how to bypass this whole mess by using a shell script to access the fast user switching instead. this is easier to script and works slighty faster than that apple applet because it doesn't use GUI scripting for that part of the script (GUI scripting is always slow). I'll post the modified version of the applet in a bit.

    P.S. when you are replying to me could you press the reply button next to the post you are replying to? makes it easier to understand who and which post you are responding to.
  • Glassix Level 1 Level 1 (0 points)
    Hey thanks for pointing out that reply button. I actually didn't see it there, instead going up to the top to reply. It was annoying too because I wasn't able to reference the last post when I was typing my response. Still learning...
  • V.K. Level 9 Level 9 (56,110 points)
    Glassix wrote:
    Hey thanks for pointing out that reply button. I actually didn't see it there, instead going up to the top to reply. It was annoying too because I wasn't able to reference the last post when I was typing my response. Still learning...


    No problem. here is my modified version of that apple applet. It's a bit quicker and it works regardless of whether you are using short or long login names in it. Either one will work.

    Paste it into Script Editor and save as main.scpt. Then right-click on the apple applet, select "show package contents" and navigate to Contents/Resources/Scripts.
    Replace the existing file main.scpt with mine.

    <pre style="
    font-family: Monaco, 'Courier New', Courier, monospace;
    font-size: 10px;
    margin: 0px;
    padding: 5px;
    border: 1px solid #000000;
    width: 720px; height: 335px;
    color: #000000;
    background-color: #ADD8E6;
    overflow: auto;"
    title="this text can be pasted into the Script Editor">
    property target_username : ""
    property target_userpassword : ""

    on open these_items
    display_main()
    end open

    on run
    try
    if target_username is "" or target_userpassword is "" then
    display_main()
    else
    set the current_user to short user name of (get system info)
    tell application "System Events"
    if UI elements enabled is false then
    set UI elements enabled to true
    end if
    if UI elements enabled is true then
    set us_id to do shell script "id -u  " & quoted form of target_username
    do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID " & us_id
    delay 0.1
    keystroke target_userpassword
    delay 0.1
    keystroke return
    end if
    end tell
    end if
    on error error_message number error_number
    if the error_number is not -128 then
    display dialog error_message with icon 2 buttons {"Cancel"} default button 1
    end if
    end try
    end run

    on display_main()
    set version_string to (the short version of (info for (path to me))) as string
    set this_iconfile to (path to resource "AccountSwitcher.icns")
    set the title_string to ("Account Switcher" & space & "v" & version_string)
    repeat
    activate
    display dialog "This application will automate switching between user accounts on this computer." & return & return & "To use, make sure Fast User Switching is enabled in the Accounts System Preference pane under Login Options." & return & return & "After setting the Fast User Switching options, click the “Set Login Info” button below to enter the name and password for the account you wish to use this application to switch to." & return & return & "To access this dialog again, drag any disk item onto this application’s icon." with icon this_iconfile with title title_string buttons {"Open Accounts", "Set Login Info", "OK"} default button 3
    set the button_pressed to the button returned of the result
    if the button_pressed is "Open Accounts" then
    tell application "System Preferences"
    set the current pane to pane id "com.apple.preferences.users"
    activate
    end tell
    exit repeat
    else if the button_pressed is "Set Login Info" then
    display dialog "Enter the  name of the targeted accounts as  it appears in the Accounts menu:" default answer target_username with icon this_iconfile with title title_string
    set the target_username to the text returned of the result
    repeat
    display dialog "Enter the password for the targeted account:" default answer "" with icon this_iconfile with title title_string with hidden answer
    set the password_01 to the text returned of the result
    display dialog "Enter the password for the targeted account again:" default answer "" with icon this_iconfile with title title_string with hidden answer
    set the password_02 to the text returned of the result
    if password_01 is not password_02 then
    display dialog "The two passwords do not match. Try again." with icon this_iconfile with title title_string
    else
    set target_userpassword to password_01
    exit repeat
    end if
    end repeat
    display dialog "The account information has been stored in this application." & return & return & "Click the OK button in the following dialog to complete the process. The next time this application is launched, it will use the stored information to switch to the target account." buttons {"OK"} default button 1 with icon this_iconfile with title title_string
    else
    exit repeat
    end if
    end repeat
    end display_main

    </pre>
  • Glassix Level 1 Level 1 (0 points)
    Couldn't get it to work, but it's probably because I don't have any passwords set up. When asked for them in Account Switcher I left the fields blank, which it seemed to accept. But when I ran the applet it just took me back to the first instructions screen. I am curious if your new version will work faster. Is there a way to make it work without using passwords? I know, I know...I should be more concerned about passwords, but it's a secure environment and I hate typing them in every time I switch users.
  • V.K. Level 9 Level 9 (56,110 points)
    you REALLY shouldn't be doing this but if you want to use it with an empty password, change the script to the following one:


    <pre style="
    font-family: Monaco, 'Courier New', Courier, monospace;
    font-size: 10px;
    margin: 0px;
    padding: 5px;
    border: 1px solid #000000;
    width: 720px; height: 335px;
    color: #000000;
    background-color: #ADD8E6;
    overflow: auto;"
    title="this text can be pasted into the Script Editor">
    property target_username : ""
    property target_userpassword : ""

    on open these_items
    display_main()
    end open

    on run
    try
    if target_username is "" then
    display_main()
    else
    set the current_user to short user name of (get system info)
    tell application "System Events"
    if UI elements enabled is false then
    set UI elements enabled to true
    end if
    if UI elements enabled is true then
    set us_id to do shell script "id -u  " & quoted form of target_username
    do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID " & us_id
    delay 0.1
    keystroke target_userpassword
    delay 0.1
    keystroke return
    end if
    end tell
    end if
    on error error_message number error_number
    if the error_number is not -128 then
    display dialog error_message with icon 2 buttons {"Cancel"} default button 1
    end if
    end try
    end run

    on display_main()
    set version_string to (the short version of (info for (path to me))) as string
    set this_iconfile to (path to resource "AccountSwitcher.icns")
    set the title_string to ("Account Switcher" & space & "v" & version_string)
    repeat
    activate
    display dialog "This application will automate switching between user accounts on this computer." & return & return & "To use, make sure Fast User Switching is enabled in the Accounts System Preference pane under Login Options." & return & return & "After setting the Fast User Switching options, click the “Set Login Info” button below to enter the name and password for the account you wish to use this application to switch to." & return & return & "To access this dialog again, drag any disk item onto this application’s icon." with icon this_iconfile with title title_string buttons {"Open Accounts", "Set Login Info", "OK"} default button 3
    set the button_pressed to the button returned of the result
    if the button_pressed is "Open Accounts" then
    tell application "System Preferences"
    set the current pane to pane id "com.apple.preferences.users"
    activate
    end tell
    exit repeat
    else if the button_pressed is "Set Login Info" then
    display dialog "Enter the  name of the targeted accounts as  it appears in the Accounts menu:" default answer target_username with icon this_iconfile with title title_string
    set the target_username to the text returned of the result
    repeat
    display dialog "Enter the password for the targeted account:" default answer "" with icon this_iconfile with title title_string with hidden answer
    set the password_01 to the text returned of the result
    display dialog "Enter the password for the targeted account again:" default answer "" with icon this_iconfile with title title_string with hidden answer
    set the password_02 to the text returned of the result
    if password_01 is not password_02 then
    display dialog "The two passwords do not match. Try again." with icon this_iconfile with title title_string
    else
    set target_userpassword to password_01
    exit repeat
    end if
    end repeat
    display dialog "The account information has been stored in this application." & return & return & "Click the OK button in the following dialog to complete the process. The next time this application is launched, it will use the stored information to switch to the target account." buttons {"OK"} default button 1 with icon this_iconfile with title title_string
    else
    exit repeat
    end if
    end repeat
    end display_main

    </pre>
Previous 1 2 Next