Multi-line, wrapped text on a Control Page

Posted on
Thu Mar 19, 2015 9:09 pm
raneil offline
User avatar
Posts: 88
Joined: Feb 11, 2011
Location: Grapevine, Texas

Multi-line, wrapped text on a Control Page

Hopefully, the new Indigo Touch 2.0 update will include a new text item that will allow multi-line, wrapped text to be included on Control Pages. Until then, here's one way to skin that cat. It's by no means perfect for every use case, but it works for my latest project. Maybe someone else will find it useful as well.

Background:

I've been working on a new thermostat Control Page. Along the way, I decided to incorporate the current weather conditions (obtained using the excellent WUnderground Plugin) in the display area at the top. I also wanted to see and review any pending Severe Weather Alerts issued for my area. The WUnderground Plugin includes up to four pending Severe Weather Alerts with each download, and each Alert includes a full (and often lengthy) description.

If there are any Alerts pending, a small Alert icon appears in the upper right corner of the display area, right next to the current weather conditions:


Image



Tapping the alert icon switches to another Control Page showing the details of the first pending Alert:


Image


If there is a second pending Alert, a small button appears in the upper right corner of the first Alert page. Tapping that button advances to an identical Alert page displaying the second Alert. And so on, up to four Alerts.

In order to include the full Alert Descriptions on each Alert Control Page, I use an AppleScript which converts the Alert description text to a PDF, and then converts the PDF to an image file. The Control Page includes a large Refreshing Image URL item. The URL points to the local image file created on the fly by the AppleScript when the Alert Control Page is initially displayed.


Code: Select all
-- the "watched" folder into which the Alert text files are created by a script on the Indigo Server, triggering the Launch Agent to run this script
set sourceFolder to ((path to documents folder) as text) & "Wunderground Alert Files:"

-- the destination folder where the converted PNG fill will be saved
set destFolder to ((path to library folder) as text) & "Application Support:Perceptive Automation:Indigo 6:IndigoWebServer:images:"

-- temporary folder in which to save the intermediate PDF
set tempFolder to (path to temporary items folder) as text

-- get a list of Alert Text files to be converted
set theseItems to {}
try
   set theseItems to paragraphs of (do shell script "ls " & (tEsc(sourceFolder) & "*.txt"))
end try

-- iterate through he list
repeat with textFile in theseItems
   try
      -- get the name of this text file; we'll use it as the name of the output PNG file as well
      set textFileQuotedPath to the quoted form of textFile
      set fileName to do shell script ("basename " & textFileQuotedPath & " '.txt'")
      
      -- save the intermediate PDF file to a temporary folder
      set thePDFFile to tempFolder & "temp.pdf"
      set pdfFileQuotedPath to the quoted form of the POSIX path of thePDFFile
      
      --set the font size and line spacing
      set fScale to 1
      set fh to 8.5
      set fh to fh * fScale
      set fw to 8
      set fw to fw * fScale
      set ls to 3.0
      
      --set the page margins; set the left margin so that when the Image Events crops the image from the center, the text fills the image
      set lm to 140
      set rm to 5
      set tm to 5
      set bm to 5
      
      --set the page orientation
      --set po to "r" -- landscape
      set po to "R" -- portrait
      
      -- convert the text file to a PDF
      set theCommand to "cat " & textFileQuotedPath & "  | enscript -fMonaco" & fw & "/" & fh & " -" & po & "lqs" & ls & " --non-printable-format=space --margins=" & lm & ":" & rm & ":" & tm & ":" & bm & " -o - | pstopdf -i -o " & pdfFileQuotedPath
      set shellResult to do shell script theCommand
      
      -- delete the text file from the "watched" folder
      do shell script "rm " & textFileQuotedPath
      
      -- convert the PDF to a png and crop it so that it will fill the designated space on the Control Page
      set hCrop to 0.4
      
      -- update the Indigo variable used to display the "Refresh" button and the "Refreshing…" message on the Alerts Control Page(s)
      tell application "IndigoServer" to set the value of variable "w_alertsActive" to "2"
      
      set the thePNGFile to destFolder & fileName & ".png"
      set the theJPGFile to tempFolder & fileName & ".jpg"
      tell application "Image Events"
         -- start the Image Events application
         launch
         -- open the PDF file
         set this_image to open thePDFFile
         
         -- get dimensions of the image
         copy dimensions of this_image to {W, H}
         
         -- crop it
         crop this_image to dimensions {W - (W * hCrop), H}
         
         -- save it as a JPG to ensure the image has an opaque white background
         save this_image as JPEG in theJPGFile with icon
         
         -- close it
         close this_image
         
         -- open the JPEG file
         set this_image to open theJPGFile
         
         -- save it as a PNG (the Refreshing Image URL item on the control page will point to this file)
         save this_image as PNG in thePNGFile with icon
         
         -- close it
         close this_image
      end tell
      
   on error errMsg number errNum
      set fullErrMsg to errMsg & " [" & (errNum as text) & "]."
   end try
end repeat

-- delete any invisible files (prevents this script from being invoked unnecessarily -- launchd will invoke this script repeatedly as long as there are ANY files in the folder, including hidden files)
try
   set theCommand to "rm -f " & tEsc(sourceFolder) & ".*"
   set ignoreThis to do shell script theCommand
end try


on tEsc(inputString)
   -- Terminal Escape; return a path string that is "escaped" for use Terminal.app
   -- Useful for creating path strings to which glob characters can be appended for use with 'do shell script'
   set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ""}
   try
      set inputString to the POSIX path of inputString
      --the list of characters to be escaped; the backslash character must be first
      --this list was compiled via LIMITED trial and error; may need to be updated.
      set charsToEscape to "\\ ~`!#$%&*()=<>,;'{}[]|" & quote
      set theString to inputString as string
      repeat with theChar in charsToEscape
         if theString contains theChar then
            --replace every occurence of the character with the escaped version
            set AppleScript's text item delimiters to theChar
            set theList to every text item of theString
            set AppleScript's text item delimiters to "\\" & theChar
            set theString to theList as text
         end if
      end repeat
   end try
   set AppleScript's text item delimiters to TID
   return theString
end tEsc


There are, of course, a few potential gotchas:

  • Apple stopped including the "enscript" command-line utility with Mac OS X sometime after Snow Leopard. I'm not sure which Mac OS X version was the last to include it. For more current versions, you'll need to download the "enscript" source code and build it yourself, or locate a pre-built binary. I have used MacPorts successfully on several Macs running Mavericks with no issues.
  • Image Events can only crop images in the center; as such, I had to use a large left margin and a small font size in the PDF so as to offset the text such that it was centered horizontally on the PDF "page". It took a fair bit of fiddling with margins, fonts, and font sizes to get the results I was after, but it was worth it.

Posted on
Fri Mar 20, 2015 3:59 am
DaveL17 offline
User avatar
Posts: 4252
Joined: Aug 20, 2013

Re: Multi-line, wrapped text on a Control Page

Wow @raneil, what an elegant solution. My only complaint is that I'll have to wait until this weekend to have a run at it. :D Thanks for posting this.

Thanks too for the kind words on the WUnderground plugin.
Dave

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

[My Plugins] - [My Forums]

Posted on
Fri Mar 20, 2015 6:05 am
raneil offline
User avatar
Posts: 88
Joined: Feb 11, 2011
Location: Grapevine, Texas

Re: Multi-line, wrapped text on a Control Page

Let me know how it works out, Dave. And thanks for the WUnderground Plugin -- love it!

Posted on
Fri Mar 20, 2015 7:50 am
Korey offline
User avatar
Posts: 503
Joined: Jun 04, 2008
Location: Henderson, NV

Re: Multi-line, wrapped text on a Control Page

raneil wrote:
Hopefully, the new Indigo Touch 2.0 update will include a new text item that will allow multi-line, wrapped text to be included on Control Pages. Until then, here's one way to skin that cat. It's by no means perfect for every use case, but it works for my latest project. Maybe someone else will find it useful as well.

Background:

I've been working on a new thermostat Control Page. Along the way, I decided to incorporate the current weather conditions (obtained using the excellent WUnderground Plugin) in the display area at the top. I also wanted to see and review any pending Severe Weather Alerts issued for my area. The WUnderground Plugin includes up to four pending Severe Weather Alerts with each download, and each Alert includes a full (and often lengthy) description.

If there are any Alerts pending, a small Alert icon appears in the upper right corner of the display area, right next to the current weather conditions:


Image



Amazing page layout! this is one of the nicest control pages I have seen!

Do you design your own graphics?

Gorgeous!

:D :D :D

--
Korey

Posted on
Fri Mar 20, 2015 9:36 am
raneil offline
User avatar
Posts: 88
Joined: Feb 11, 2011
Location: Grapevine, Texas

Re: Multi-line, wrapped text on a Control Page

Korey wrote:
Amazing page layout! this is one of the nicest control pages I have seen!

Do you design your own graphics?

Gorgeous!

:D :D :D


Thanks, Korey. Making custom Control Pages is one of my favorite things about Indigo.

But I can't take full credit for the graphics. What I do is less "designing" and more modifying the designs of others to suit my purposes. I start with a Photoshop file like this one that contains professionally designed iPhone graphics. Using modified elements from that file, I make a full mockup of my Control Page in Photoshop, then transfer each element to the Control Page one at a time. It's tedious work, but I think it's worth the effort.

Posted on
Thu Aug 30, 2018 3:26 pm
bsp9493 offline
Posts: 90
Joined: Nov 30, 2017
Location: Kelowna, BC

Re: Multi-line, wrapped text on a Control Page

hi...

This is EXACTLY what I have been looking for, only difference I am using a trigger to create a file to be displayed on a control page.

does this solution still work with indigo 7?

If so, I have a couple newbie questions...

I see you have included code in applescript (not familiar with this at all unfortunatley) :(

I have created a test file in documents/Wunderground Alert Files/
alerttest.txt

I have created a variable named w_alertsActive.

I have updated the reference to indigo 6

I have copied your code to a trigger action be executed using apple script.
I compile the code (all good)
I run the code - there is a pause, alertest.txt is deleted, w_alertsActive is updated to 2 but I don't get an output file.

What am i missing??

Thanx in advance

Dave

Posted on
Thu Aug 30, 2018 5:52 pm
Different Computers offline
User avatar
Posts: 1846
Joined: Jan 02, 2016
Location: East Coast

Re: Multi-line, wrapped text on a Control Page

Just in case that way of doing things doesn't work for you or has become broken, allow me to point you to the OS X Automator "Create Banner Image from Text" action.

SmartThings refugee, so happy to be on Indigo. 10.13.6 on an i5 MBP w/Harmony Hub, Hue, DomoPad, Dynamic URL, Device Extensions, HomeKit Bridge, MatplotLib, Plex, uniFAP, Wunderground, Nanoleaf, LED Simple Effects, Airfoil Pro, Grafana.

Posted on
Fri Aug 31, 2018 8:25 am
bsp9493 offline
Posts: 90
Joined: Nov 30, 2017
Location: Kelowna, BC

Re: Multi-line, wrapped text on a Control Page

trying to learn and debug at the same time...

newbie question related to the applescript in this thread.

as a trigger, using applescript, this seems to fail on this line...

Code: Select all
set theCommand to "cat " & textFileQuotedPath & "   | enscript -fMonaco" & fw & "/" & fh & " -" & po & "lqs" & ls & " --non-printable-format=space --margins=" & lm & ":" & rm & ":" & tm & ":" & bm & " -o - | pstopdf -i -o " & pdfFileQuotedPath
set shellResult to do shell script theCommand


the reply I get from apples Script editor is as follows.
Code: Select all
do shell script "cat '/Users/davidmorhun/Documents/Wunderground Alert Files/alert.txt'   | enscript -fMonaco8/8.5 -Rlqs3.0 --non-printable-format=space --margins=140:5:5:5 -o - | pstopdf -i -o '/Users/davidmorhun/Documents/Wunderground Alert Files/tmp/temp.pdf'"
      --> error "sh: enscript: command not found
pstopdf failed on PS read from stdin with error code -30998" number 234


again, just learning, but when I take this same line that is generated by the applescript above and put it directly into terminal, it executes with no error.
Code: Select all
"cat '/Users/davidmorhun/Documents/Wunderground Alert Files/alert.txt'   | enscript -fMonaco8/8.5 -Rlqs3.0 --non-printable-format=space --margins=140:5:5:5 -o - | pstopdf -i -o '/Users/davidmorhun/Documents/Wunderground Alert Files/tmp/temp.pdf'"


I am confused. why will this work if I run it in terminal, but not work if I run the applescript via a trigger in indigo?

Thanx in advance for the help.

dave

Posted on
Fri Aug 31, 2018 8:46 am
jay (support) offline
Site Admin
User avatar
Posts: 14773
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Multi-line, wrapped text on a Control Page

Hi Dave, welcome to the Indigo community. I would highly recommend that you not become dependent on this script. We will be deprecating AppleScript (in favor of Python) in an upcoming Indigo release (likely 7.3, which will be the feature release after 7.2 which we're releasing soon).

There are a variety of other solutions for multiline text on a control page - search on "multiline text". Here's one post with some pointers to a couple of different approaches.

You can also post a question over on the Control Pages subforum with the specifics of what you're trying to do (describe the scenario without thinking about implementation) - I'm sure you'll get a lot of people chiming in on possible solutions.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Aug 31, 2018 8:50 am
kw123 offline
User avatar
Posts: 5923
Joined: May 12, 2013
Location: Dallas, TX

Re: Multi-line, wrapped text on a Control Page

You need to put the full path to the programs into the indigo script.
It’s a Unix thing. When you open a normal terminal all paths are set, when you create a shell command it’s a naked environment with nothing set, ie no path

All most everyone did this mistake.

Karl



Sent from my iPhone using Tapatalk

Posted on
Fri Aug 31, 2018 9:01 am
jay (support) offline
Site Admin
User avatar
Posts: 14773
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Multi-line, wrapped text on a Control Page

FYI, here's the problem you're seeing:

raneil wrote:
Apple stopped including the "enscript" command-line utility with Mac OS X sometime after Snow Leopard. I'm not sure which Mac OS X version was the last to include it. For more current versions, you'll need to download the "enscript" source code and build it yourself, or locate a pre-built binary. I have used MacPorts successfully on several Macs running Mavericks with no issues.


I would recommend that you not install/use MacPorts - we've had several people accidentally install MacPorts versions of Python (because of dependencies in MacPorts) which will wreak havoc with Indigo and is a mess to clean up. Just a word of warning if you're not a very technical user.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Aug 31, 2018 10:47 am
bsp9493 offline
Posts: 90
Joined: Nov 30, 2017
Location: Kelowna, BC

Re: Multi-line, wrapped text on a Control Page

Thanx for the input.

I was aware that applescript was being deprecated, but thought I would give this solution a try in the interim as it is much more elegant than what I have been using modified from another user post.

I was aware of a couple of the other options that you pointed to (via threads), but totally missed the matplot functionaliy! Thanx for that! - not pretty (because of the input text I am using, but it works.) AND, it is super easy!

You guys are GREAT!

Thanx again for the help.

d

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest