Tuesday, September 29, 2020

#1 2020-09-15 04:03:24 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 399

Easy way to debug Operators in AS.

I made this to make it easier for newcomer to debug Operators in AppleScript.
It take 2 values input1 and input2

If the input compare is false it will output every statement of Operators as list.

This example use the Operator is in in the list its index 17 (object 17) in the list.
It compare the string variable a and b and return boolean value.

Anything you set for the variable a and b will change the statement of Operator list.

Applescript:

set a to "hello world" as text
set b to "some other text with hello world" as text

-- Set the index in the list of Operators.

-- ex. Index 17 = 'is in' (a string is in b string)
set idx to 17 --> true

try
   if (item idx of (my compareOperator:a withInput:b)) then
       return (item idx of (my compareOperator:a withInput:b))
   else
       -- show every statement when its false
       return (my compareOperator:a withInput:b)
   end if
end try

on compareOperator:input1 withInput:input2
   try
       return {(input1 > input2) as boolean, ¬
           (input1 < input2) as boolean, ¬
           (input1 = input2) as boolean, ¬
           (input1 ≤ input2) as boolean, ¬
           (input1 ≥ input2) as boolean, ¬
           (input1 ≠ input2) as boolean, ¬
           (input1 is greater than input2) as boolean, ¬
           (input1 is not greater than input2) as boolean, ¬
           (input1 is greater than or equal to input2) as boolean, ¬
           (input1 is not greater than or equal to input2) as boolean, ¬
           (input1 is less than input2) as boolean, ¬
           (input1 is equal to input2) as boolean, ¬
           (input1 is less than or equal to input2) as boolean, ¬
           (input1 is not less than or equal to input2) as boolean, ¬
           (input1 is not equal to input2) as boolean, ¬
           (input1 contains input2) as boolean, ¬
           (input1 is in input2) as boolean, ¬
           (input1 is not in input2) as boolean, ¬
           (input1 does not contain input2) as boolean, ¬
           (input1 comes after input2) as boolean, ¬
           (input1 does not come after input2) as boolean, ¬
           (input1 comes before input2) as boolean, ¬
           (input1 does not come before input2) as boolean, ¬
           (input1 starts with input2) as boolean, ¬
           (input1 ends with input2) as boolean, ¬
           (class of input1 is equal to class of input2) as boolean}
   end try
end compareOperator:withInput:


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

#2 2020-09-16 02:06:18 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 399

Re: Easy way to debug Operators in AS.

In this example I show 3 different Operators that AppleScript support.

As you could see it give different results.

Applescript:

use framework "AppKit"

(**
* [Class]: NSFontManager
*    The center of activity for the font-conversion system.
*
* [Instance Property]: availableFonts
*    The names of the fonts available in the system
*    (not the NSFont objects themselves).
*)

-- return a list of availableFonts
set manager to current application's NSFontManager's sharedFontManager()
-- return (manager's availableFonts()) as list

-- ex.
set fontNameToCheck to "Verdana"
set fontList to (manager's availableFonts()) as list
repeat with i from 1 to (count fontList)
   -- Ex1. give 2 hits.
   if ((item i of fontList) is in fontNameToCheck) then
       set itemName1 to (item i of fontList)
       log "Font name of first example: " & itemName1
   end if
   -- ex2. give 2 hits.
   if ((item i of fontList) contains fontNameToCheck) then
       set itemName2 to (item i of fontList)
       log "Font name of second example: " & itemName2
   end if
   -- ex3. give 1 hits.
   if ((item i of fontList) is equal to fontNameToCheck) then
       set itemName3 to (item i of fontList)
       log "Font name of third example: " & itemName3
   end if
end repeat

(*
   If you search for font name only first and third example give that.

   If you search for font name and like to have output of every style.
   The second example in repeat loop will give that.
*)


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)