Apple Script Help

Discussion in 'Mac Programming' started by bluereef, Mar 17, 2013.

  1. macrumors member

    Joined:
    Jan 29, 2012
    #1
    Would someone be so kind as to help me with a code problem.

    set weatherWindChillSpeed to COL_YELLOW & "Like: " & weatherWindChill & degrees & " Outside Right Now / Wind Speed Is: " & weatherWindSpeed & " " & speed & COL_RESET & newline & newline & COL_YELLOW & "Wind Direction From The: " & weatherWindDirection

    Comes out like this:

    "Like: 76° F Outside Right Now / Wind Speed Is: 12 mph

    Wind Direction From The: South South East

    Relative Humidity : 40% / Visibility Distance: 10 mi

     Atmospheric Pressure: 30.09 in - Rising: 2 in"

    What I want to do is to indent : WIND DIRECTION FROM THE SOUTH SOUTH EAST

    So It would be centered instead of having it indented to the left
     
  2. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    Please post the entire code and use code tags.
     
  3. thread starter macrumors member

    Joined:
    Jan 29, 2012
    #3
    Total Script

    Code:
     white_space : {space, tab, return, (ASCII character 10), (ASCII character 13)}
    on run
    	set pathName to "/Users/papabear/Documents/WeatherIcon"
    	set weatherFile to pathName & "/WeatherIcon.txt"
    	set weatherLocation to "USFL0378"
    	set tempType to "f" -- "f" for Fahrenheit and "c" for Celsius … must be lowercase
    	
    	set COL_RED to "\\\\x1b[37\\;01m"
    	set COL_GREEN to "\\\\x1b[37\\;01m"
    	set COL_YELLOW to "\\\\x1b[37\\;01m"
    	set COL_RESET to "\\\\x1b[37\\;37\\;00m"
    	set newline to "\\\\n"
    	set degreesF to "° F"
    	set degreesC to "° C"
    	set degrees to ""
    	
    	if tempType is equal to "f" then
    		set degrees to degreesF
    	else if tempType is equal to "c" then
    		set degrees to degreesC
    	end if
    	
    	set weatherCurl to (do shell script "curl --silent 'http://xml.weather.yahoo.com/forecastrss/" & weatherLocation & "_" & tempType & ".xml' | grep  'yweather:' >" & weatherFile)
    	
    	--set weatherCurl to (do shell script "curl --silent 'http://weather.yahooapis.com/forecastrss?p=" & weatherLocation & "&u=f' | grep  'yweather:' >" & weatherFile)
    	
    	set distance to (do shell script "grep -E 'yweather:units' " & weatherFile & "| sed -e 's/.*distance=//;s/pressure=.*//' | sed 's/\"//g'") as string
    	set distance to (my trim_string(distance, white_space, "both"))
    	
    	set pressure to (do shell script "grep -E 'yweather:units' " & weatherFile & "| sed -e 's/.*pressure=//;s/speed=.*//' | sed 's/\"//g'") as string
    	set pressure to (my trim_string(pressure, white_space, "both"))
    	
    	set speed to (do shell script "grep -E 'yweather:units' " & weatherFile & "| sed -e 's/.*speed=//;s/\\/>.*//' | sed 's/\"//g'") as string
    	set speed to (my trim_string(speed, white_space, "both"))
    	
    	
    	set weatherRegion to (do shell script "grep -E 'yweather:location' " & weatherFile & "| sed -e 's/.*region=//;s/country=.*//' | sed 's/\"//g'") as string
    	set weatherRegion to (my trim_string(weatherRegion, white_space, "both"))
    	
    	set weatherCountry to (do shell script "grep -E 'yweather:location' " & weatherFile & "| sed -e 's/.*country=//;s/\\/>.*//' | sed 's/\"//g'") as string
    	set weatherCountry to (my trim_string(weatherCountry, white_space, "both"))
    	
    	
    	set weatherCondition to (do shell script "grep -E 'yweather:condition' " & weatherFile & "| sed -e 's/.*text=//;s/code=.*//' | sed 's/\"//g'") as string
    	set weatherCondition to (my trim_string(weatherCondition, white_space, "both"))
    	
    	set weatherTemp to (do shell script "grep -E 'yweather:condition' " & weatherFile & "| sed -e 's/.*temp=//;s/date=.*//' | sed 's/\"//g'") as string
    	set weatherTemp to (my trim_string(weatherTemp, white_space, "both"))
    	
    	set weatherCondition to COL_YELLOW & weatherCondition & ", " & weatherTemp & degrees & COL_RESET
    	
    	set weatherWindChill to (do shell script "grep -E 'yweather:wind' " & weatherFile & "| sed -e 's/.*chill=//;s/direction=.*//' | sed 's/\"//g'") as string
    	set weatherWindChill to (my trim_string(weatherWindChill, white_space, "both"))
    	
    	set weatherWindDirection to (do shell script "grep -E 'yweather:wind' " & weatherFile & "   | sed -e 's/.*direction=//;s/speed=.*//' | sed 's/\"//g'") as string
    	set weatherWindDirection to (my trim_string(weatherWindDirection, white_space, "both"))
    	set weatherWindDirection to (my windDirection(weatherWindDirection))
    	
    	set weatherWindSpeed to (do shell script "grep -E 'yweather:wind' " & weatherFile & "| sed -e 's/.*speed=//;s/\\/>.*//' | sed 's/\"//g'") as string
    	set weatherWindSpeed to (my trim_string(weatherWindSpeed, white_space, "both"))
    	
    	set weatherWindChillSpeed to COL_YELLOW & "Like: " & weatherWindChill & degrees & " Outside Right Now /       Wind Speed Is: " & weatherWindSpeed & " " & speed & COL_RESET & newline & newline & COL_YELLOW & "Wind Direction From The: " & weatherWindDirection
    	
    	
    	
    	
    	set weatherHumidity to (do shell script "grep -E 'yweather:atmosphere' " & weatherFile & "| sed -e 's/.*humidity=//;s/visibility=.*//' | sed 's/\"//g'") as string
    	set weatherHumidity to (my trim_string(weatherHumidity, white_space, "both"))
    	
    	set weatherVisibilty to (do shell script "grep -E 'yweather:atmosphere' " & weatherFile & "| sed -e 's/.*visibility=//;s/pressure=.*//' | sed 's/\"//g'") as string
    	set weatherVisibilty to (my trim_string(weatherVisibilty, white_space, "both"))
    	
    	set weatherPressure to (do shell script "grep -E 'yweather:atmosphere' " & weatherFile & "| sed -e 's/.*pressure=//;s/rising=.*//' | sed 's/\"//g'") as string
    	set weatherPressure to (my trim_string(weatherPressure, white_space, "both"))
    	
    	set weatherRising to (do shell script "grep -E 'yweather:atmosphere' " & weatherFile & "| sed -e 's/.*rising=//;s/\\/>.*//' | sed 's/\"//g'") as string
    	set weatherRising to (my trim_string(weatherRising, white_space, "both"))
    	
    	--set weatherAtmosphere to COL_YELLOW & "Humdity: " & weatherHumidity & "%    / Visibility: " & weatherVisibilty & " " & distance & "    /    Pressure:" & weatherPressure & " " & pressure & "    -    Rising:" & weatherRising & " " & pressure & COL_RESET
    	
    	set weatherAtmosphere to COL_YELLOW & "Relative Humidity  : " & weatherHumidity & "% / Visibility Distance: " & weatherVisibilty & " " & distance & COL_RESET & newline & newline & COL_YELLOW & "      Atmospheric Pressure: " & weatherPressure & " " & pressure & " - Rising: " & weatherRising & " " & pressure & COL_RESET
    	
    	set weatherIcon to (do shell script "grep -E 'yweather:condition' " & weatherFile & "| sed -e 's/.*code=//;s/temp=.*//' | sed 's/\"//g'") as string
    	set weatherIcon to (my trim_string(weatherIcon, white_space, "both"))
    	
    	do shell script ("cp " & pathName & "/png/" & weatherIcon & ".png " & pathName & "/WeatherIcon.png")
    	
    	do shell script ("echo " & weatherWindChillSpeed & newline & newline & weatherAtmosphere)
    	
    end run
    
    on trim_string(the_string, trim_chars, trim_parameter)
    	set start_char to 1
    	set end_char to length of the_string
    	set all_chars to (characters of the_string)
    	
    	if trim_parameter is in {"left", "both"} then
    		repeat with each_char in all_chars
    			if each_char is not in trim_chars then exit repeat
    			set start_char to (start_char + 1)
    		end repeat
    	end if
    	
    	if trim_parameter is in {"right", "both"} then
    		set all_chars to reverse of all_chars
    		repeat with each_char in all_chars
    			if each_char is not in trim_chars then exit repeat
    			set end_char to (end_char - 1)
    		end repeat
    	end if
    	
    	try
    		return text start_char thru end_char of the_string
    	on error
    		return ""
    	end try
    end trim_string
    
    on windDirection(theDegrees)
    	
    	set degrees to theDegrees as number
    	
    	if (degrees ≥ 0 and degrees ≤ 11.25) then
    		return "North"
    	end if
    	if degrees > 11.25 and degrees ≤ 33.75 then
    		return "North North East"
    	end if
    	if (degrees > 33.75 and degrees ≤ 56.25) then
    		return "North East"
    	end if
    	if (degrees > 56.25 and degrees ≤ 78.75) then
    		return "East North East"
    	end if
    	if (degrees > 78.75 and degrees ≤ 101.25) then
    		return "East"
    	end if
    	if (degrees > 101.25 and degrees ≤ 123.75) then
    		return "East South East"
    	end if
    	if (degrees > 123.75 and degrees ≤ 146.25) then
    		return "South East"
    	end if
    	if (degrees > 146.25 and degrees ≤ 168.75) then
    		return "South South East"
    	end if
    	if (degrees > 168.75 and degrees ≤ 191.25) then
    		return "South"
    	end if
    	if (degrees > 191.25 and degrees ≤ 213.75) then
    		return "South South West"
    	end if
    	if (degrees > 213.75 and degrees ≤ 236.25) then
    		return "South West"
    	end if
    	if (degrees > 236.25 and degrees ≤ 258.75) then
    		return "West South West"
    	end if
    	if (degrees > 258.75 and degrees ≤ 281.25) then
    		return "West"
    	end if
    	if (degrees > 281.25 and degrees ≤ 303.75) then
    		return "West North West"
    	end if
    	if (degrees > 303.75 and degrees ≤ 326.25) then
    		return "North West"
    	end if
    	if (degrees > 326.25 and degrees ≤ 348.75) then
    		return "North North West"
    	end if
    	if (degrees > 348.75 and degrees ≤ 360) then
    		return "North"
    	end if
    	
    	return ""
    	
    end windDirection
     
  4. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4
    I could be wrong but I think there's no way of text formatting a string in pure Applescript. As you also can see your ANSI color codes don't quite produce the output you thought they would. I'd look into something other than Applescript if you want to color and format strings.
     
  5. thread starter macrumors member

    Joined:
    Jan 29, 2012
    #5
    Script

    I did it before but cannot remember how I did it...
     

    Attached Files:

  6. thread starter macrumors member

    Joined:
    Jan 29, 2012

Share This Page