Convert to Python?

Posted on
Mon Apr 02, 2018 2:34 pm
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Convert to Python?

Is something like this AS convertible to Python?

Code: Select all
set someString to value of variable "CID_Display_Name"
set noTrailingSpacesString to removeTrailingSpaces(someString)

on removeTrailingSpaces(theString)
   repeat while theString ends with space
      set theString to text 1 thru -2 of theString
   end repeat
   --return theString
   set the value of variable "CID_Display_Name" to theString
end removeTrailingSpaces


Appreciate any help.

Thanks,

Carl

Posted on
Mon Apr 02, 2018 2:44 pm
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Convert to Python?

Code: Select all
no_trailing_spaces_string = indigo.variables[VARIDHERE].value.rstrip()
no_leading_spaces_string = indigo.variables[VARIDHERE].value.lstrip()
no_leading_or_trailing_spaces_string = indigo.variables[VARIDHERE].value.strip()
no_spaces_at_all_string = indigo.variables[VARIDHERE].value.replace(" ", "")


Python is so much better at string manipulation than AppleScript is. And with collections (lists, dictionaries, etc.). And with... ;)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Apr 02, 2018 3:01 pm
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Convert to Python?

Wow Jay, that’s impresssive.
Many thanks!

I have a number of this type of script that based on what you’ve done here I think I can convert.

Carl

Posted on
Mon Apr 02, 2018 3:06 pm
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Convert to Python?

Can I get greedy and do one more?
This is another I use a lot of.

Code: Select all
tell application "IndigoServer"
   set the value of variable "GrowlAlert" to value of variable "CID_Display_Name" & " called at " & (value of variable "ClockTimeAmPm") & " " & (value of variable "CID_Display_Number") & " on " & (value of variable "DayShort") & " " & (value of variable "MonthShort") & " " & (value of variable "DateExt")
   
end tell


Many thanks,

Carl

Posted on
Mon Apr 02, 2018 3:53 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Convert to Python?

Code: Select all
new_string = "{} called at {} {} on {} {} {}".format(
    indigo.variables["CID_Display_Name"].value,
    indigo.variables["ClockTimeAmPm"].value,
    indigo.variables["CID_Display_Number"].value,
    indigo.variables["DayShort"].value,
    indigo.variables["MonthShort"].value,
    indigo.variables["DateExt"].value
)
indigo.variable.updateValue( "GrowlAlert", new_string)


But you're better off using the ID of the variable than the name.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Apr 02, 2018 4:05 pm
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Convert to Python?

FlyingDiver wrote:
But you're better off using the ID of the variable than the name.


+1 - you should always use IDs (which don't change) rather than names (that do change).

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Apr 02, 2018 4:06 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Convert to Python?

jay (support) wrote:
FlyingDiver wrote:
But you're better off using the ID of the variable than the name.


+1 - you should always use IDs (which don't change) rather than names (that do change).


I was being lazy. :)

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Apr 02, 2018 4:08 pm
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Convert to Python?

FlyingDiver wrote:
I was being lazy. :)


I edited your script so it's a bit easier to read (and because that little info box overlaps code blocks - still haven't figured out how to fix that in the CSS).

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Apr 02, 2018 4:10 pm
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Convert to Python?

By the way, another thing that Python is SIGNIFICANTLY better at is dates and times - calculating, formatting, etc., is all a lot more flexible in Python. Not sure how those variables are getting populated, but you may be able to save yourself some time and variables if they're only there as a holding place for their calculation.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Apr 02, 2018 4:14 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Convert to Python?

I've also taken to doing it like this to improve clarity a little:
Code: Select all
f_string = "{} called at {} {} on {} {} {}"
new_string = f_string.format(
    indigo.variables["CID_Display_Name"].value,
    indigo.variables["ClockTimeAmPm"].value,
    indigo.variables["CID_Display_Number"].value,
    indigo.variables["DayShort"].value,
    indigo.variables["MonthShort"].value,
    indigo.variables["DateExt"].value
)
indigo.variable.updateValue( "GrowlAlert", new_string)

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Apr 02, 2018 4:48 pm
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Convert to Python?

Thanks, really appreciate all the help!

I should be able to convert a good chunk of my AS’s.

Carl

Posted on
Mon Apr 02, 2018 7:22 pm
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Convert to Python?

jay (support) wrote:
I edited your script so it's a bit easier to read (and because that little info box overlaps code blocks - still haven't figured out how to fix that in the CSS).

The author block doesn't overlap the code box in Safari (v11.1 anyway). It does shorten it, but it doesn't obscure it.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Mon Apr 02, 2018 7:23 pm
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Convert to Python?

Although now that I think about it, the fact that I have visible scroll bars may have something to do with that.
Attachments
Screen Shot 2018-04-02 at 8.22.33 PM.png
Screen Shot 2018-04-02 at 8.22.33 PM.png (20.88 KiB) Viewed 2376 times

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Apr 03, 2018 9:16 am
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Convert to Python?

Yeah, what I'd prefer is to have the code block start wrapping rather than scrolling under block. I'm not a CSS expert (and I think I'd probably go insane if I had to become one).

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Apr 03, 2018 9:35 am
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: Convert to Python?

jay (support) wrote:
Yeah, what I'd prefer is to have the code block start wrapping rather than scrolling under block.

But if you wrapped the line then it wouldn't read like the original code. I actually like the scrolling because I see the code verbatim.

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest

cron