Jump to content

Coding clean up


Recommended Posts

Hello,

I not good at all in Applescripting and I would like to utilize  the large text display in Alfred. Also my coding is a bit sloppy... Any suggestions would be great.  

 

I am making a workflow to convert a date into roman numeral date. Once done will be posting the finished results.. 

on alfred_script(q)
try
	tell (current date)
		set yearInt to its year
		set monthInt to its month as integer
		set dayInt to its day
	end tell
	
	set defaultDateString to (dayInt &  monthInt & yearInt) as text
	
	tell current application
		set userDateString to text returned of (display dialog "Enter a date of the form: DD/MM/YYYY" with title "AppleScript By Christopher Stone" default answer defaultDateString as text)
	end tell
	
	if userDateString ≠ defaultDateString then
		set AppleScript's text item delimiters to "/"
		set {dayInt, monthInt, yearInt} to text items of userDateString
	end if
	
on error e number n
	set e to e & return & return & "Num: " & n
	if n ≠ -128 then
		try
			tell current application to button returned of ¬
				(display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
					default button "OK" giving up after 30)
			if ddButton = "Copy" then set the clipboard to e
		end try
	end if
end try


try
	
	set RomanYear to ""
	repeat with i from 1 to (count (yearInt as string))
		set RomanYear to item (((item -i of (yearInt as string)) as integer) + 1) of item i of ¬
			{{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}, ¬
				{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}, ¬
				{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}, ¬
				{"", "M", "MM", "MMM"}} & RomanYear
	end repeat
	set RomanMonth to item monthInt of {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XII", "XII"}
	set RomanDay to item dayInt of {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX", "XX", "XXI", "XXII", "XXXII", "XXIV", "XXV", "XXVI", "XXVII", "XXVIII", "XXIX", "XXX", "XXXI"}
	set the clipboard to RomanDay & "-" & RomanMonth & "-" & RomanYear
	
	display notification RomanDay & "-" & RomanMonth & "-" & RomanYear with title "Roman Numerles set to Clipboard" sound name "Submarine"
	
on error e number n
	set e to e & return & return & "Num: " & n
	if n ≠ -128 then
		try
			tell current application to button returned of ¬
				(display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
					default button "OK" giving up after 30)
			if ddButton = "Copy" then set the clipboard to e
		end try
	end if
end try
end alfred_script

Much appreciated,

Bill

 

Link to comment

Hello,

I not good at all in Applescripting and I would like to utilize  the large text display in Alfred. Also my coding is a bit sloppy... Any suggestions would be great.  

 

I am making a workflow to convert a date into roman numeral date. Once done will be posting the finished results.. 

on alfred_script(q)
try
	tell (current date)
		set yearInt to its year
		set monthInt to its month as integer
		set dayInt to its day
	end tell
	
	set defaultDateString to (dayInt &  monthInt & yearInt) as text
	
	tell current application
		set userDateString to text returned of (display dialog "Enter a date of the form: DD/MM/YYYY" with title "AppleScript By Christopher Stone" default answer defaultDateString as text)
	end tell
	
	if userDateString ≠ defaultDateString then
		set AppleScript's text item delimiters to "/"
		set {dayInt, monthInt, yearInt} to text items of userDateString
	end if
	
on error e number n
	set e to e & return & return & "Num: " & n
	if n ≠ -128 then
		try
			tell current application to button returned of ¬
				(display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
					default button "OK" giving up after 30)
			if ddButton = "Copy" then set the clipboard to e
		end try
	end if
end try


try
	
	set RomanYear to ""
	repeat with i from 1 to (count (yearInt as string))
		set RomanYear to item (((item -i of (yearInt as string)) as integer) + 1) of item i of ¬
			{{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}, ¬
				{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}, ¬
				{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}, ¬
				{"", "M", "MM", "MMM"}} & RomanYear
	end repeat
	set RomanMonth to item monthInt of {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XII", "XII"}
	set RomanDay to item dayInt of {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX", "XX", "XXI", "XXII", "XXXII", "XXIV", "XXV", "XXVI", "XXVII", "XXVIII", "XXIX", "XXX", "XXXI"}
	set the clipboard to RomanDay & "-" & RomanMonth & "-" & RomanYear
	
	display notification RomanDay & "-" & RomanMonth & "-" & RomanYear with title "Roman Numerles set to Clipboard" sound name "Submarine"
	
on error e number n
	set e to e & return & return & "Num: " & n
	if n ≠ -128 then
		try
			tell current application to button returned of ¬
				(display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
					default button "OK" giving up after 30)
			if ddButton = "Copy" then set the clipboard to e
		end try
	end if
end try
end alfred_script

Much appreciated,

Bill

 

Bill, I moved this into the workflow help section of the forums to get it a little more exposure.

 

Are you needing help with something in particular on this or simply looking for pointers?

Link to comment

I re-coded it in PHP and dropped it in a simple workflow that you can download here: https://www.dropbox.com/s/hihr5zl054wdq5u/Roman%20Date.alfredworkflow?dl=0.

 

It works as a script filter and uses PHP's date parser, so you can do things like "rd now" and "rd tomorrow" and "rd next friday" and "rd -1000 years" as well as "Oct 29 1993" or "10/29/1993".

 

Currently, if you press enter, it just displays it as large text.

Link to comment

I re-coded it in PHP and dropped it in a simple workflow that you can download here: https://www.dropbox.com/s/hihr5zl054wdq5u/Roman%20Date.alfredworkflow?dl=0.

 

It works as a script filter and uses PHP's date parser, so you can do things like "rd now" and "rd tomorrow" and "rd next friday" and "rd -1000 years" as well as "Oct 29 1993" or "10/29/1993".

 

Currently, if you press enter, it just displays it as large text.

 

Thank you Shawn, it works great Now I got to figure out how you did it and work on my PHP coding... 

 

Link to comment

Thank you Shawn, it works great Now I got to figure out how you did it and work on my PHP coding... 

 

After you install the workflow, just open it up and look at the `script-filter.php`. You'll see that I've included a library called "Alphred" that I wrote that makes writing workflows in PHP much, much easier, and so that is what controls the output for the script filter. Here, the library just easily creates the XML that Alfred reads.

 

The text to date functionality (for things like "now" or "tomorrow" or "+1 year") are built into PHP's date functionality. More specifically, they're built into the function `strtotime`, and so that does a lot of the heavy lifting.

 

The actual conversion from Arabic to Roman numerals is done by breaking the date string (31/3/1999) into an array and then using an `array_filter` (which just applies the same function to each part). So, all we needed was a single function to convert an Arabic number to a Roman numeral.

 

The function that does that is here:

function toRoman( $integer ) {
    $output = '';
    $conversion = [
    	'M' => 1000,
    	'CM'=> 900,
    	'D'=>500,
    	'CD'=>400,
    	'C'=>100,
    	'XC'=>90,
    	'L'=>50,
    	'XL'=>40,
    	'X'=>10,
    	'IX'=>9,
    	'V'=>5,
    	'IV'=>4,
    	'I'=>1
    ];

	while ( $integer > 0 ) :
		foreach ( $conversion as $roman => $arabic ) :
			if ( $integer >= $arabic ) {
				$integer -= $arabic;
				$output .= $roman;
				break;
			}
		endforeach;
	endwhile;

	return $output;
}

So, basically it takes an associative array that defines the translations between Arabic and Roman and then just uses two loops to modify the input value while constructing an output string. Each time it adds onto the string, it removes the equivalent value from the input integer and just keeps going until the integer reaches 0 and then returns the string.

 

Hopefully the explanation helps.

 

I did decide to make it a script filter rather than using an Applescript input box because it makes it easier to manipulate the values quickly and see the results.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...