Applescript and ANSI color codes

Hi, I would like to know how to use the ANSI colors. First, is it possible? I’ve seen someone use it with Ruby and in Shell, but I would like to make it work with Applescript. I’m using Applescript with Geektool by the way… :stuck_out_tongue:
Oh, and I’m what a lot of you would call a newbie… :smiley:

Hello.

This is really a thing you wouldn’t use AppleScript for Ruby works in the shell, so it is quite native for Ruby to handle ansi escape sequences .

The only way for AppleScript to handleansi escape sequences , is through the shell, so you are really adding and extra layer here.

Aside from that, you would have to make your incantations from AppleScript be persistent to the shell process in speak, and that would be near to impossible in most cases.

I advise you to go for the shell or Ruby or any similar tool in your case.

Ok, so its virtually impossible right? I better learn Ruby then if I want to do what I want! Wish me luck! :smiley:
I’m scared now… :o

Hello.

I don’ know what you are exactly up to, but it would be easier to use the shell, and google around for some snippets.

Good luck

Well, I just want to use this script :

set displayLogic to "enhanced alt2"

tell application "System Events"
	set powerCheck to ((application processes whose (name is equal to "iTunes")) count)
	if powerCheck = 0 then
		return "✖"
	else
		
		-- This section moved to avoid launching iTunes when the script is run.		
		tell application "iTunes"
			try
				set playerstate to (get player state)
			end try
			if playerstate = paused then
				set trackStatus to "❚❚ "
			else
				set trackStatus to "â–¶ "
			end if
			
			try
				set trackID to the current track
				set trackplaylist to name of current playlist
				set trackName to the name of trackID
				set artistName to the artist of trackID
				set albumName to the album of trackID
				set tracktime to the time of trackID
				set trackplaycount to the played count of trackID
				set ratetrack to the rating of trackID
				if ratetrack < 20 then
					set trackrating to "☆"
				else
					if ratetrack < 40 then
						set trackrating to "★"
					else
						if ratetrack < 60 then
							set trackrating to "2★"
						else
							if ratetrack < 80 then
								set trackrating to "3★"
							else
								if ratetrack < 100 then
									set trackrating to "4★"
								else
									set trackrating to "5★"
								end if
							end if
						end if
					end if
				end if
				--set trackdur to the duration of trackID
				--set trackdur to round {trackdur}
				--set trackskip to the skipped count of trackID
				
				
				if displayLogic = "enhanced alt2" then
					if playerstate = paused then
						set displaymain to "❚❚" & " | " & trackplaylist
					else
						set displaymain to trackStatus & trackName & " | " & artistName & " | " & albumName & " | (" & tracktime & ") | " & trackrating & " | " & trackplaycount & "x" & " | " & trackplaylist
					end if
				else
					set displaymain to trackStatus & " " & trackName & " | " & artistName
				end if
			on error
				if playerstate = stopped then
					set trackStatus to "â—¼"
				end if
				
			end try
		end tell
	end if
end tell

and have the artist (or anything else) showing up in another color… It’s really basic and non-vital. so if it takes you a long time to figure it out, just never mind! I’ll find another way! :stuck_out_tongue:
thanks a lot!

Hello

Where do you want it to display the colors together with what text from that script in what order.

-What do you want it to look like?
-And exactly how do you want it to work?
:slight_smile:

well, like i said, im using geektool… so it would be on my desktop. the only thing I would like to be in another color would be a specific variable (the albumName for example).
if you dont know geektool, well it just refreshes the script every x amount of seconds and shows it on the desktop…
thanks again! McUsr! :slight_smile:

Hello.

I’ll come back to you with something within 24 hours. Or a negative answer if the ansi colors doesn’t work be with geektool, but I think the odds are in favor of that they do.

thanks a lot! :smiley:

Hello.

I have made a shell script for you with an embedded osascript which talks to itunes.
You would store this somewhere and supply the full path to the file ansi.codes2.

You should get the hang of how to define your own colors.
I’ll see if I can speed it up a little bit tomorrow.

[code]#!/bin/bash
source ansi.codes2
osascript <itunes.tmp
tell application “System Events”
set powerCheck to ((application processes whose (name is equal to “iTunes”)) count)
if powerCheck = 0 then
return “✖”
else

	-- This section moved to avoid launching iTunes when the script is run.		
	tell application "iTunes"
		try
			set playerstate to (get player state)
		end try
		if playerstate = paused then
			set trackStatus to "❚❚ "
		else
			set trackStatus to "â–¶ "
		end if
		
		try
			set trackID to the current track
			set trackplaylist to name of current playlist
			set trackName to the name of trackID
			set artistName to the artist of trackID
			set albumName to the album of trackID
			set tracktime to the time of trackID
			set trackplaycount to the played count of trackID
			set ratetrack to the rating of trackID
			if ratetrack < 20 then
				set trackrating to "☆"
			else
				if ratetrack < 40 then
					set trackrating to "★"
				else
					if ratetrack < 60 then
						set trackrating to "2★"
					else
						if ratetrack < 80 then
							set trackrating to "3★"
						else
							if ratetrack < 100 then
								set trackrating to "4★"
							else
								set trackrating to "5★"
							end if
						end if
					end if
				end if
			end if
			--set trackdur to the duration of trackID
			--set trackdur to round {trackdur}
			--set trackskip to the skipped count of trackID
			if playerstate = paused then
				set displaymain to "❚❚" & " | " & trackplaylist
			else
				set displaymain to trackStatus & trackName & " ; " & artistName & " ; " & albumName & " ; " & " ( " & tracktime & ")" & ";" & trackrating & " ; " & trackplaycount & "x" & " ; " & trackplaylist
			end if
			
		on error
			if playerstate = stopped then
				set displaymain to "â—¼"
			end if
			
		end try
	end tell
end if

end tell
displaymain
!EOF

REST=cat itunes.tmp | awk 'BEGIN { FS=";"} { print NF }'
if [ $REST -eq “7” ]; then
MagentaOnBlue
echo -n cat itunes.tmp |awk 'BEGIN { FS=";"} { print $1 }'
WhiOnBlue
echo -n " | "
MagentaOnBlue
echo -n cat itunes.tmp |awk 'BEGIN { FS=";"} { print $2 }'
WhiOnBlue
echo -n " | "
MagentaOnBlue
echo -n cat itunes.tmp |awk 'BEGIN { FS=";"} { print $3 }'
WhiOnBlue
echo -n " | "
MagentaOnBlue
echo -n cat itunes.tmp |awk 'BEGIN { FS=";"} { print $4 }'
WhiOnBlue
echo -n " | "
BlackOnBlue
echo -n cat itunes.tmp |awk 'BEGIN { FS=";"} { print $5 }'
WhiOnBlue
echo -n " | "
MagentaOnBlue
echo -n cat itunes.tmp |awk 'BEGIN { FS=";"} { print $6 }'
WhiOnBlue
echo -n " | "
echo -n cat itunes.tmp |awk 'BEGIN { FS=";"} { print $7 }'
fi[/code]
And here is the ansi.codes2 file.

[code]#! /bin/bash

McUsr’s ansi escape sequences.

I gather some one can make up their own functions for the Bash shell by looking at these.

ESC=printf "\e"
ScreenRedraw_off=echo -n "$ESC""[8m"
ScreenRedraw_on=echo -n "$ESC""[0m"
DisableAutoLineWrap=echo -n "$ESC""[7h"
EnableAutoLineWrap=echo -n "$ESC""[7l"
ResetAllAttributes=echo -n "$ESC""[0m"

#----------------------------------- Font Attributes
FontBold=echo -n "$ESC""[1m"
FontBright=echo -n "$ESC""[1m"
FontDim=echo -n "$ESC""[2m"
FontStandOut=echo -n "$ESC""[3m"
FontUnderlined=echo -n "$ESC""[4m"
FontBlinking=echo -n "$ESC""[5m"
InverseVideo=echo -n "$ESC""[7m"
DisableBrightOrDim=echo -n "$ESC""[22m"
DisableStandout=echo -n "$ESC""[23m"
DisableUnderlined=echo -n "$ESC""[24m"
DisableBlinking=echo -n "$ESC""[25m"
DisableReverseVideo=echo -n "$ESC""[26m"

------------------------------------ Foreground Color

TextColorBlack=echo -n "$ESC""[30m"
TextColorRed=echo -n "$ESC""[31m"
TextColorGreen=echo -n "$ESC""[32m"
TextcolorYellow=echo -n "$ESC""[33m"
TextColorBlue=echo -n "$ESC""[34m"
TextColorMagenta=echo -n "$ESC""[35m"
TextColorCyan=echo -n "$ESC""[36m"
TextColorWhite=echo -n "$ESC""[37m"
TextColorDefault=echo -n "$ESC""[39m"

------------------------------------ Background Color

TextBckgBlack=echo -n "$ESC""[40m"
TextBckgRed=echo -n "$ESC""[41m"
TextBckgGreen=echo -n "$ESC""[42m"
TextBckgYellow=echo -n "$ESC""[43m"
TextBckgBlue=echo -n "$ESC""[44m"
TextBckgMagenta=echo -n "$ESC""[45m"
TextBckgCyan=echo -n "$ESC""[46m"
TextBckgWhite=echo -n "$ESC""[47m"
TextBckgDefault=echo -n "$ESC""[49m"

------------------------------------- Other Attributes

txtDef=echo -n "$ESC""[39m""$ESC""[49m"
txtErr=echo -n "$ESC""[37m""$ESC""[41m"

------------------------------------- Functions

function WhiOnRed()
{
echo -n ${TextColorWhite}${TextBckgRed}
}
function WhiOnBlue()
{
echo -n ${TextColorWhite}${TextBckgBlue}
}
function YellowBlue()
{
echo -n ${TextcolorYellow}${TextBckgBlue}
}
function BlackOnBlue()
{
echo -n ${TextColorBlack}${TextBckgBlue}
}

function CyanOnBlue()
{
echo -n ${TextColorCyan}${TextBckgBlue}
}
function MagentaOnBlue()
{
echo -n ${TextColorMagenta}${TextBckgBlue}
}
function ResetCrt()
{
echo -n ${ResetAllAttributes}
}

function terminalDefault()
{
echo -n ${TextColorDefault}${TextBckgDefault}“”
}

— usage of a function

CyanOnBlue # ← That’s it.[/code]

Hmm. thanks a lot, but I don’t know how to make it work! :confused:
I got the part where you have to save the ansi.codes2 somewhere, and then put the shell script. But how do I put the artist red for example? I (kinda) understand what you’ve done, but I can’t use it (yet)! Like I said… I’m really a newbie! :stuck_out_tongue:

Hello.

First of all you have to add the posix path to the ansi.codes2 to where it resides, and for best results, that would be to some catalog without any spaces in them.

f.ex: /users/you/bin

Here is a function that you can add to the ansi.codes2 file, which prints what comes after with red on white background, if you study this function with what is in the ansi.codes2 file already then you should get the hang of it.

If you study the names in ansi.codes2 then you should be able to find some correlations
The rest of this is left as an exercise for you.

# insert this into ansi.codes2, and replace some other color function call in the actual script with RedOnWhi function RedOnWhi() { echo -n ${TextColorRed}${TextBckgWhite} }
The major piece with the osascript for calling itunes should be pasted into the geeklet I think, or you can put it in the same bin folder in a separate file named for instance itunegeek.sh
Recipe:
Save contents in posts under respective filenames in /users/you/bin folder
open a terminal window:
enter cd bin (enter)
then
enter chmod u+x itunegeek.sh (enter)
then enter the full filename to this into your geeklet. aka /users/you/bin/itunegeek.sh
:smiley: You gonna be so proud of your self when you have figured it all out! :slight_smile:

sorry for the late answer… i got really busy lately! :stuck_out_tongue:
haven’t have time to try it yet… but as soon as i do, ill let you know! thanks a lot once again! :smiley: