Multi-line, wrapped text on a Control Page

Plugins and other miscellany for Indigo.
User avatar
raneil
Posts: 90
Joined: Fri Feb 11, 2011 12:06 pm
Location: Grapevine, Texas

Multi-line, wrapped text on a Control Page

Post by raneil »

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.
User avatar
DaveL17
Posts: 6881
Joined: Tue Aug 20, 2013 11:02 am
Location: Chicago, IL, USA
Contact:

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

Post by DaveL17 »

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]
User avatar
raneil
Posts: 90
Joined: Fri Feb 11, 2011 12:06 pm
Location: Grapevine, Texas

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

Post by raneil »

Let me know how it works out, Dave. And thanks for the WUnderground Plugin -- love it!
User avatar
Korey
Posts: 824
Joined: Wed Jun 04, 2008 11:47 am
Location: Henderson, NV
Contact:

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

Post by Korey »

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
User avatar
raneil
Posts: 90
Joined: Fri Feb 11, 2011 12:06 pm
Location: Grapevine, Texas

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

Post by raneil »

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.
bsp9493
Posts: 153
Joined: Thu Nov 30, 2017 12:09 pm
Location: Kelowna, BC

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

Post by bsp9493 »

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
User avatar
Different Computers
Posts: 2609
Joined: Sat Jan 02, 2016 10:07 am
Location: East Coast
Contact:

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

Post by Different Computers »

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.
Sonoma on a Mac Mini M1 running Airfoil Pro, Bond Home, Camect, Roku Network Remote, Hue Lights, DomoPad, Adapters, Home Assistant Agent, HomeKitLinkSiri, EPS Smart Dimmer, Fantastic Weather, Nanoleaf, LED Simple Effects, Grafana. UnifiAP
bsp9493
Posts: 153
Joined: Thu Nov 30, 2017 12:09 pm
Location: Kelowna, BC

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

Post by bsp9493 »

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
User avatar
jay (support)
Site Admin
Posts: 18411
Joined: Wed Mar 19, 2008 11:52 am
Location: Austin, Texas
Contact:

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

Post by jay (support) »

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
User avatar
kw123
Posts: 8453
Joined: Sun May 12, 2013 4:44 pm
Location: Dallas, TX
Contact:

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

Post by kw123 »

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
User avatar
jay (support)
Site Admin
Posts: 18411
Joined: Wed Mar 19, 2008 11:52 am
Location: Austin, Texas
Contact:

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

Post by jay (support) »

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
bsp9493
Posts: 153
Joined: Thu Nov 30, 2017 12:09 pm
Location: Kelowna, BC

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

Post by bsp9493 »

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
Post Reply

Return to “raneil's corner”