Sunday, December 4, 2022

#1 2022-11-14 01:11:29 pm

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

Simple Swift Vs AppleScript

One way to learn is to study something else, it could give inpiration to think different.
I have always been intresting to study other programming language,

AppleScript --> AppleScriptObjC --> Objective-C --> Swift
                                 |
                                 |
                                 ---> Python --> PyObjC

And JavaScript in between for other approaches.

So I decide to do some basic things in Xcode and Swift and do the same in AppleScript.
Its the early thing of mine, but its fun... The code in comments is Swift code tested in Xcode.

Applescript:

use framework "Foundation"
use scripting additions

(**
* The Swift programming language version 5.7
* Category: String and Character
*
* Reference: docs.swift.org
*
* The code have been tested in Xcode 14.1
*
* To print a string in Xcode console we use print(someString)
*)


(**
* String Literals
*)


-- Swift
-- let someString = "Some string literal value"

-- AppleScript
set someString to "Some string literal value"
-- log someString

(*
   Swift String Mutability

This works
   var variableString = "Horse"
   variableString += " and carriage"

This doesn't work Error: Left side of mutating operator isn't mutable: 'constString' is a 'let' constant
   let constantString = "Highlander"
   constantString += " and another Highlander"
*)


-- AppleScript
set theString to "Horse"
set theString to theString & " and carriage"
-- log theString

(*
   Swift Working with Character
   
   for character in "Dog" {
       print(character)
   }
*)


-- AppleScript
repeat with char in "Dog"
   -- log char
end repeat

(*
   Control Flow - For-In Loops

This example uses a for-in loop to iterate over the items in an array:

let names = ["Anna","Alex","Brian","Jack"]
   for name in names {
print("Hello, \(name)!")
}
*)


-- AppleScript
repeat with name in {"Anna", "Alex", "Brian", "Jack"}
   -- log "Hello, " & name & "!"
end repeat

(*
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {
print("\(animalName)s have \(legCount) legs")
}
*)


-- AppleScript
set numberOfLegs to {spider:8, ant:6, cat:4}

-- AS couldn't give us the keys so we need to do something more.
set allKeys to ((current application's NSDictionary's dictionaryWithDictionary:numberOfLegs)'s allKeys()) as list

set animalName to allKeys
-- The values we could get by convert AS record to list
set legCount to numberOfLegs as list

repeat with i from 1 to count animalName
   -- log (item i of animalName) & "s" & " have " & item i of legCount & " legs"
end repeat

(*
for index in 1...5 {
print("\(index) times 5 is \(index * 5)")
}
*)


-- AppleScript
repeat with i from 1 to 5
   log i & " times 5 is " & i * 5
end repeat

(*
let base = 3
let power = 10
var answer = 1
for _ in 1...power {
answer *= base
}
print("\(base) to the power of \(power) is \(answer)")
*)


set {base, power, answer} to {3, 10, 1}
repeat with i from 1 to power
   set answer to answer * base
end repeat
--> 3 to the power of 10 is 59049
-- log base & " to the power of " & power & " is " & answer

(*
let minutes = 60
for tickMark in 0..<minutes {
print(tickMark)
}
*)


set theMinutes to 60
repeat with i from 0 to theMinutes
   -- log i
end repeat

(*
let minuteInterval = 5
for tickMark in stride(from: 0, to: minutes, by: minuteInterval) {
print(tickMark)
}
*)


set minuteInterval to 5
repeat with i from 0 to theMinutes by minuteInterval
   -- log i
end repeat

(*
let hours = 12
let hoursInterval = 3
for tickMark in stride(from: 3, through: hours, by: hoursInterval) {
print(tickMark)
}
*)


set hours to 12
set hoursInterval to 3
repeat with i from 0 to hours by hoursInterval
   -- log i
end repeat

Last edited by Fredrik71 (2022-11-14 01:13:39 pm)


Node-RED makes it easy to automate IoT

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)