Thursday, February 25, 2021

#1 2021-01-17 12:00:16 pm

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

PyObjC, NSDateComponentsFormatter

With this example I like to show 2 things. The approach is not about speed.

1. We will use PyObjC to access CFPreferencesCopyAppValue from CoreFoundation (Foundation)
2. We will change the interval of seconds to (minutes/seconds).

https://developer.apple.com/documentati … guage=objc

This example have been tested with Python3, and the latests PyObjC 7.1

Applescript:

use framework "Foundation"
use scripting additions

set formatter to current application's NSDateComponentsFormatter's alloc()'s init()
return ("The idle time: " & (formatter's stringFromTimeInterval:myfunction()) as string) & " in minutes and seconds"

on myfunction()
   set theScript to "#!/usr/bin/env python3
from Foundation import *
print Foundation.CFPreferencesCopyAppValue('idleTime','com.apple.screensaver')
"

   return (do shell script "python <<EOF " & theScript & "EOF") as string
end myfunction

Here is example in Swift, if you wonder what idleTime is...
its the time in seconds for Start after: <5 Minutes> in Desktop & Screen Saver from System Preferences pane. To access this information from Apple API we use Core Foundation API.
It use CFString and in Swift we could convert it with: as CFString . In AppleScriptObjC its
not possible to do that. Also if you use as CFString you also need to use Foundation framework. In the end of the function in the print statement I use ! to remove optional type.

The swift version is faster and PyObjC.

Applescript:

set theScript to "
import Foundation
print(CFPreferencesCopyAppValue(\"idleTime\" as CFString,\"com.apple.screensaver\" as CFString)!)
"

set thePath to POSIX path of (path to temporary items)

my runSwiftWithString:theScript inPath:thePath

on runSwiftWithString:theSource inPath:thePath
   set theSourceName to "source.swift"
   set theResult to (do shell script "echo " & quoted form of theSource & " > " & thePath & theSourceName & ";" & space & "swift " & thePath & theSourceName)
end runSwiftWithString:inPath:

Applescript:

use framework "Foundation"
use scripting additions

set theScript to "
import Foundation
print(CFPreferencesCopyAppValue(\"idleTime\" as CFString,\"com.apple.screensaver\" as CFString)!)
"

set thePath to POSIX path of (path to temporary items)

set myfunction to my runSwiftWithString:theScript inPath:thePath

set formatter to current application's NSDateComponentsFormatter's alloc()'s init()
return ("The idle time: " & (formatter's stringFromTimeInterval:myfunction) as string) & " in minutes and seconds"

on runSwiftWithString:theSource inPath:thePath
   set theSourceName to "source.swift"
   set theResult to (do shell script "echo " & quoted form of theSource & " > " & thePath & theSourceName & ";" & space & "swift " & thePath & theSourceName)
end runSwiftWithString:inPath:

Last edited by Fredrik71 (2021-01-17 04:09:36 pm)


The purpose to study someone else art is not to add, its to make less more.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)