Setting the next letter of the alphabet as a variable

Setting a script to mount a user’s network folder, all of which are housed within 13 shares named similar to (a01-b01, c01-d01, e01-f01, etc.). Taking the first letter of their username, I need to tell applescript what the next letter in the alphabet is past that letter, and then send them to the correct variable to locate their folder. What I have thus far:

=======
tell application “System Events”
set shortName to name of current user

--Set the next variable using bash to ascertain the sub-folder of the server
set Mark to do shell script "echo $(whoami | cut -c 1)"
set Grp1 to {"a", "c", "e", "g", "i", "k", "m", "o", "q", "s", "u", "w", "y"}
set Grp2 to {"b", "d", "f", "h", "j", "l", "n", "p", "r", "t", "v", "x", "z"}

end tell

set verifyShortName to display dialog “Please verify that your account name is correct” default answer shortName with icon 2 buttons {“Cancel”, “OK”} default button {“OK”}
set shortName to text returned of verifyShortName

if Mark is in Grp1 then
set SubFolder to (Mark & “0” & “1” & “-” & (select (Mark) + 1) & “0” & “1”)
else if Mark is in Grp2 then
set SubFolder to ((select (Mark) - 1) & “0” & “1” & Mark & “0” & “1”)
end if

– Mount the Network Home Directory (U Drive).
tell application “Finder”
try
mount volume “smb://network/” & SubFolder & “$/” & shortName & “/”

end try

end tell

The script fails on finding the next letter (select (Mark) + 1). Is there a way to tell applescript to use the variable Mark and insert the next letter from the alphabetic (and ANSII) order?

Model: MacBook Pro (13-inch, Mid-2012)
AppleScript: 2.3.2
Browser: Safari 537.85.10
Operating System: Other

Hi,

do you mean this


set shortName to short user name of (system info)
set verifyShortName to display dialog "Please verify that your account name is correct" default answer shortName with icon 2 buttons {"Cancel", "OK"} default button {"OK"}
set shortName to text returned of verifyShortName
set prefixID to id of first character of shortName
set SubFolder to (character id prefixID & "01" & "-" & character id (prefixID + 1) & "01")

what’s supposed to happen if the first character is “z” ??

If the first character is “z,” it should be directed to the last group as it would if it were “y”. Good catch, I need to adjust that.

set Mark to do shell script "echo $(whoami | cut -c 1)"
set Grp1 to {"a", "c", "e", "g", "i", "k", "m", "o", "q", "s", "u", "w", "y"}
set Grp2 to {"b", "d", "f", "h", "j", "l", "n", "p", "r", "t", "v", "x"}
set Grp3 to {"z"}

end tell

set verifyShortName to display dialog “Please verify that your account name is correct, your 5-2-1-dash-‘s’.” default answer shortName with icon 2 buttons {“Cancel”, “OK”} default button {“OK”}
set shortName to text returned of verifyShortName

if Mark is in Grp1 then
set SubFolder to (Mark & “0” & “1” & “-” & (select (Mark) + 1) & “0” & “1”)
else if Mark is in Grp2 then
set SubFolder to ((select (Mark) - 1) & “0” & “1” & Mark & “0” & “1”)
else if Mark is in Grp3 then
set SubFolder to (“y” & “0” & “1” & “-” & “z” & “0” & “1”)
end if

Getting closer, though having difficulty getting the ASCII code to convert back to character using the character id command:

=======
tell application “System Events”
set shortName to name of current user

--Set the next variable using bash to ascertain the sub-folder of the server
set Char to do shell script "echo $(whoami | cut -c 1)"
set upNum to (ASCII number Char) + 1
set downNum to (ASCII number Char) - 1
set Grp1 to {"a", "c", "e", "g", "i", "k", "m", "o", "q", "s", "u", "w", "y"}
set Grp2 to {"b", "d", "f", "h", "j", "l", "n", "p", "r", "t", "v", "x"}
set Grp3 to {"z"}
set upChar to character id (upNum)
set downChar to character id (downNum)

end tell

set verifyShortName to display dialog “Please verify that your account name is correct” default answer shortName with icon 2 buttons {“Cancel”, “OK”} default button {“OK”}
set shortName to text returned of verifyShortName

if Char is in Grp1 then
set SubFolder to (Char & “0” & “1” & “-” & upChar & “0” & “1”)
else if Char is in Grp2 then
set SubFolder to (downChar & “0” & “1” & Char & “0” & “1”)
else if Char is in Grp3 then
set SubFolder to (“y” & “0” & “1” & “-” & “z” & “0” & “1”)
end if

Model: MacBook Pro (13-inch, Mid-2012)
AppleScript: 2.3.2
Browser: Safari 537.85.10
Operating System: Other

Hello

As I am curious, I’m wondering what’s the need for
set Grp2 to {“b”, “d”, “f”, “h”, “j”, “l”, “n”, “p”, “r”, “t”, “v”, “x”}
set Grp3 to {“z”}

If I concatenate the two lists in a single one :
set Grp2 to {“b”, “d”, “f”, “h”, “j”, “l”, “n”, “p”, “r”, “t”, “v”, “x”, “z”}
I get the same results.

So it seems that you may use :

tell application "System Events"
	set shortName to name of current user
end tell
# There is no reason to leave the trailing instruction in the tell "System Events" block !
--Set the next variable using bash to ascertain the sub-folder of the server
--set Char to do shell script "echo $(whoami | cut -c 1)"
set Char to "z"
set upNum to (id of Char) + 1 # EDITED
set downNum to (id of Char) - 1 # EDITED
set Grp1 to {"a", "c", "e", "g", "i", "k", "m", "o", "q", "s", "u", "w", "y"}
set Grp2 to {"b", "d", "f", "h", "j", "l", "n", "p", "r", "t", "v", "x", "z"}

set upChar to character id (upNum)
set downChar to character id (downNum)


set verifyShortName to display dialog "Please verify that your account name is correct" default answer shortName with icon 2 buttons {"Cancel", "OK"} default button {"OK"}
set shortName to text returned of verifyShortName

if Char is in Grp1 then
	set SubFolder to (Char & "0" & "1" & "-" & upChar & "0" & "1")
else if Char is in Grp2 then
	set SubFolder to (downChar & "0" & "1" & Char & "0" & "1")
end if

Yvan KOENIG (VALLAURIS, France) mardi 16 décembre 2014 22:22:43

Oops, when editing the script I forgot to remove the calls to the deprecated ASCII.
Thanks to StefanK it’s now corrected :wink:

ASCII number is deprecated since 10.5 (like info for ;))

from the Leopard release notes
id of “A” returns 65, and character id 65 returns “A”

Two variables were unneeded.

tell application "System Events"
	set shortName to name of current user
end tell
# There is no reason to leave the trailing instruction in the tell "System Events" block !
--Set the next variable using bash to ascertain the sub-folder of the server
--set Char to do shell script "echo $(whoami | cut -c 1)"
set Char to "z"
set upChar to character id ((id of Char) + 1) # EDITED
set downChar to character id ((id of Char) - 1) # EDITED
set Grp1 to {"a", "c", "e", "g", "i", "k", "m", "o", "q", "s", "u", "w", "y"}
set Grp2 to {"b", "d", "f", "h", "j", "l", "n", "p", "r", "t", "v", "x", "z"}

set verifyShortName to display dialog "Please verify that your account name is correct" default answer shortName with icon 2 buttons {"Cancel", "OK"} default button {"OK"}
set shortName to text returned of verifyShortName

if Char is in Grp1 then
	set SubFolder to (Char & "0" & "1" & "-" & upChar & "0" & "1")
else if Char is in Grp2 then
	set SubFolder to (downChar & "0" & "1" & Char & "0" & "1")
end if

Yvan KOENIG (VALLAURIS, France) mercredi 17 décembre 2014 09:27:02

a version without lookup tables


set shortName to short user name of (system info)
set verifyShortName to display dialog "Please verify that your account name is correct" default answer shortName with icon 2 buttons {"Cancel", "OK"} default button {"OK"}
set shortName to text returned of verifyShortName
set prefix to character 1 of shortName
set numValue to id of prefix
if numValue < 97 or numValue > 122 then return
if numValue mod 2 = 0 then
	set SubFolder to (character id (numValue - 1) & "01-" & prefix & "01")
else
	set SubFolder to (prefix & "01-" & character id (numValue + 1) & "01")
end if

Stefan’s with a shorter ending:


set shortName to short user name of (system info)
set verifyShortName to display dialog "Please verify that your account name is correct" default answer shortName with icon 2 buttons {"Cancel", "OK"} default button {"OK"}
set shortName to text returned of verifyShortName
set prefix to character 1 of shortName
set numValue to (id of prefix)
if numValue < 97 or numValue > 122 then return
tell numValue + numValue mod 2 to set SubFolder to (character id (it - 1) & "01-" & character id it & "01")

Thanks, Yvan! That works well. I will test also with the other replies, as there is discussion over keeping the ranges (or adjusting the numbers) and changing the username convention.

Thanks again for everyone’s help.