Split string in a variable?

Dear Folks,

serialport listing will give back as result

{“/dev/cu.modem”, “/dev/cu.HUAWEIMobile-Modem”, “/dev/cu.HUAWEIMobile-Pcui”, “/dev/cu.HUAWEIMobile-Diag”}

How can I handle the variable “test” to be only “/dev/cu.HUAWEIMobile-Modem”? This should be done dynamically - I will always know the name of the device, but not the position as there can be instead of HUAWEI Novatel or Option for example…

Thanks for any suggestions…

Stefan

Hi,

It looks deceptively simple although it may not be.

Anyway, I am proceeding with the assumption (based on the look of it) that what you have in your serial port listing is a list itself of items of class string. Therefore, setting your variable “test” to an indexed item of your serial port listing should give you the item that you want.

For example, if your serial port listing is stored in a list (let us say “serialPortList”), then you can set your variable “test” like so: set test to item 2 of serialPortList. This one-liner should give you item 2 of your serial port list which is: “/dev/cu.HUAWEIMobile-Modem” but only if the position of the string is constant, ie., always the second item.

If the position of the string “/dev/cu.HUAWEIMobile-Modem” dynamically changes, you can let your script cycle through the list and if the item is equal to “/dev/cu.HUAWEIMobile-Modem”, then have that item stored in your variable “test”. A repeat block such as:


set i to 0
set test to ""
repeat with i from 1 to count of serialPortList
if item i is equal to "/dev/cu.HUAWEIMobile-Modem" then
set test to item i
exit repeat
else
set i to i + 1
end repeat

You can also use the alternative repeat block such as: repeat with modemItem in serialPortList…and so on.

I hope the above helps.

Good luck.

archseed :slight_smile:

Dear archseed,

thanks for your help this works great!

Best Regards,

Stefan

Hey Stefan,

Thanks for the feedback.

I’m glad it worked out alright.

Good luck on your project.

archseed :slight_smile:

Archseed, it appears you have combined two methods into one.
If you have a repeat loop in the form of repeat with i from 1 to 5, then you don’t need to increment the i within the loop as well.

Here is a method without the line:

on indexOfListItem(theList, listItem)
	if theList does not contain listItem then return 0
	
	set c to count of theList
	repeat with i from 1 to c
		tell item i of theList to if it is listItem then return i
	end repeat
end indexOfListItem

And another using the alternative repeat method you said:

on indexOfListItem(theList, listItem)
	if theList does not contain listItem then return 0
	
	set i to 1
	repeat with thisItem in theList
		if contents of thisItem is listItem then return i
		set i to i + 1
	end repeat
end indexOfListItem

Hi Querty,

thanks for your feedback - I first have played with archseeds script but came now (of course with your help) to the following:


set theList to serialport list
set listItem to "/dev/cu.HUAWEIMobile-Modem"
on indexOfListItem(theList, listItem)
	if theList does not contain listItem then return 0
	
	set c to count of theList
	repeat with i from 1 to c
		tell item i of theList to if it is listItem then return i
	end repeat
end indexOfListItem

Now I have to add some ifs because it can be that there is no Huawei Modem but a Novatel or Option…

Is it clever to add an if when the return is 0 - or should I duplicate that script by adding the new devices?

Thanks,

Stefan

Hi Qwerty,

it works fine Script Editor,
but in XCode I am gettin an error message “on display but end should be there”…
/Users/sl/Documents/Web’n’walk Manager MacOS/Web’n’walk Manager MacOS.applescript:41: Expected “end” but found “on”. (-2741)

I have written another one but this is only displaying the number and not the name…


set foo to serialport list
set item_num to my list_position("/dev/cu.HUAWEIMobile-Modem", foo)

on list_position(this_item, this_list)
	repeat with i from 1 to the count of this_list
		if item i of this_list is this_item then return i
	end repeat
	return 0
end list_position

How can I change the number with the name?

Thanks for your time and help

Best Regards,

Stefan

What does this mean? What are you trying to accomplish with your code, exactly? It’s unclear what you’re doing with the index you’re getting back, as it’s not used anywhere in the code you’ve posted. Are you just trying to test if the item exists in the list, or do you actually need to know the index and then use it later?

Here’s another variation, using the obj-c call method on the list (an NSArray)…

set theIndex to 0
if (theList contains listItem) then set theIndex to (call method "indexOfObject:" of theList with parameter listItem) + 1

j

Hi Jobu,

I am trying to filter the connected device from all the other ones which are connected but not
really working - f.ex /dev/cu.Modem is the internal one, /dev/cu.HUAWEIMobile-Modem is the
one which is currently connected and working…


set theList to serialport list
set listItem to "/dev/cu.HUAWEIMobile-Modem"
on indexOfListItem(theList, listItem)
   if theList does not contain listItem then return 0
   
   set c to count of theList
   repeat with i from 1 to c
       tell item i of theList to if it is listItem then return i
   end repeat
end indexOfListItem

is not really working, because when I change listItem it will not return with 0 but with the changed name…

set foo to serialport list
set item_num to my list_position("/dev/cu.HUAWEIMobile-Modem", foo)

on list_position(this_item, this_list)
   repeat with i from 1 to the count of this_list
       if item i of this_list is this_item then return i
   end repeat
   return 0
end list_position

Is working great, but it is returning this_item with a number (2 → cu.modem is the first) - I have to play a little to return with this_item as filename…

Thanks for your time…

Stefan

Hi,

Thanks Qwerty for the correction. I agree. There is no need to increment i in the repeat block. I usually don’t but for some reason, I just wanted to make things a bit simpler to grasp (though probably too simplified). In fact, I have issues with statements like “tell item i of theList to if it is listItem then return i” because it just loses that “Englishness” (if I can use the quoted term) in it. Though it does make the codes more compact and perhaps, faster to run, I still prefer the longer but less convoluted approach.

Stefan, can you try these changes and see if they work?


set theList to serialport list
set listItem to "/dev/cu.HUAWEIMobile-Modem"
on indexOfListItem(theList, listItem)
if theList does not contain listItem then return ""
repeat with i from 1 to count of listItem
tell item i of theList to if it is listItem then return item i
end repeat
end indexOfListItem

In the above, I just tried to revert to the class string (which you hinted you already did) to get away from the integer result and to return class string as you wanted.

Good luck.

archseed :slight_smile:

Hi Archseed,

another time I have to thank you for your efford,
but when I am changing listitem to something different
the output is still the “something different” - it looks
like that the script is not entering the if clause…

Thanks for your help and your effort!

Stefan

Sorry Stefan.

There was a problem with “count of listItems”. That should be “count of theList”.

Try to change and see if it works. If not, let’s go back to the drawing board and see how we can make it work.

Good luck.

archseed

Hi Archseed,

you don´t have to be sorry - I am so happy that you try to help me! Thanks for that!

Unfortuanly it even seems that the if clause will not be entered… therefore the variable is still
/dev/cu.HUAWEIMobile-Modemsillyname and the output is not " "…

Thanks,

Stefan

Hi,

Just thinking through, if the original post that I suggested did work, why not just go back to that and use it? It did work for you, didn’t it? There is no point in being too cute with coding unless it could be made to work.

About the only thing I would fix in the original post is taking out the “set i to i + 1” line in the repeat block which is superfluous as I admitted. Then, just store the item “/dev/cu.HUAWEIMobile-Modem” if contained in the list in a new variable that you can call and use in your script.

I will keep thinking about the revised post as suggested by Qwerty and see how it can be made to work. Until then, use what works for you.

Good luck.

archseed :slight_smile:

Hi Stefan,

My mind is racing right now and thought a simple change in the revised post might work. Could you try it? I’ve got no Mac here so I have to pass this on to you.

Here is the revised version:


set theList to serialport list
set listItem to "/dev/cu.HUAWEIMobile-Modem"
on indexOfListItem(theList, listItem)
set i to 0
repeat with i from 1 to count of theList
if item i is listItem then return item i
end repeat
if theList does not contain listItem then return ""
end indexOfListItem

Note that I just moved the line that checks if theList contains listItem after the repeat block. I’m not sure if it’ll work but it does seem logical to me to put it there after the script has gone thru the list. If it doesn’t, then an empty string is to be returned.

Might it work? I sure hope so for you. Else, go back to what worked before.

archseed :slight_smile:

Hi Archseed,

thank you so much for your help - My brain was also raising and I finally found a working solution:


set modem_check to serialport list
	if modem_check contains "/dev/cu.HUAWEIMobile-Modem" then
		set modem to "/dev/cu.HUAWEIMobile-Modem"
		set modem_display to "Web'n'walk Box compact"
		display dialog modem_display
		display dialog modem
		
	else if modem_check contains "/dev/cu.HUAWEIMobile-Diag" then
		set modem to "/dev/cu.HUAWEIMobile-Diag"
		set modem_display to "Kein gültiges Modem gefunden"
		return modem_display
		return modem
	end if


this works as I expected - the parts of the modem_check will be checked if modem is inside…

Thanks a lot for your help!

Stefan