Thursday, August 11, 2022

#1 2021-12-06 08:55:47 am

Registered: 2021-11-15
Posts: 17

write text on Touch Bar

at the help of @Mark FX and @Shane Stanley (Thank you) I successfully put text in the menubar using script below. I just wonder if it's possible to put the same think in the Touch Bar with minor modifications.



use framework "Foundation"
use framework "AppKit"
use scripting additions

property myApp : a reference to current application

property statusBar : missing value
property statusItem : missing value
property statusItemImage : missing value

set statusItemTitle to "Hello World! ⛵️"

set my statusBar to myApp's NSStatusBar's systemStatusBar()

if my NSThread's isMainThread() as boolean then
   my displayStatusItem:statusItemTitle
   my performSelectorOnMainThread:"displayStatusItem:" withObject:statusItemTitle waitUntilDone:true
end if

my changeStatusItemTitle:statusItemTitle
delay 3

-- display alert (statusBar's thickness())

   set statusItemTitle to (time string of (current date))
   my changeStatusItemTitle:statusItemTitle
   delay (1.0)
end repeat

on changeStatusItemTitle:title
   statusItem's button's setTitle:(title)
end changeStatusItemTitle:

on displayStatusItem:title
   set my statusItem to statusBar's statusItemWithLength:(myApp's NSVariableStatusItemLength)
   statusItem's button's setTitle:title
   set statusItemMenu to myApp's NSMenu's alloc()'s initWithTitle:""
   set quitMenuItem to myApp's NSMenuItem's alloc()'s initWithTitle:"Quit" action:"quitStatusItem" keyEquivalent:"q"
   quitMenuItem's setTarget:me
   statusItemMenu's addItem:quitMenuItem
   statusItem's setMenu:statusItemMenu
end displayStatusItem:

on quitStatusItem()
   set quitStatusItem to button returned of (display alert "Do you want to quit the status bar item ?" buttons {"NO", "YES"})
   if quitStatusItem = "YES" then
       my removeStatusItem()
       tell me to quit
   end if
end quitStatusItem

on removeStatusItem()
   statusBar's removeStatusItem:statusItem
end removeStatusItem

-- And Add This Function

on quit {}
   continue quit
end quit

([applescript] posting tag corrected by NG.)



#2 2021-12-06 08:59:32 am

Registered: 2021-11-15
Posts: 17

Re: write text on Touch Bar

I found a link below but not really understand.
Touch Bar GUI Scripting Demo … 00070.html

Last edited by aerolite (2021-12-06 09:00:45 am)



#3 2021-12-06 10:43:30 am

Mark FX
From:: UK
Registered: 2011-08-12
Posts: 163

Re: write text on Touch Bar

That link is not about creating a touch bar application, but is about how to GUI script a touch bar application, which is a big difference.

You can script other applications like "Finder" or "Safari" and loads of others, and you can do whats called GUI scripting, which is the same as clicking buttons and other UI elements within applications with AppleScript code, which is what that linked article is all about.
But to be clear, that GUI scripting code has nothing to do with actually creating an application with an NSTouchBar feature.
For that you should be Googling NSTouchBar application tutorials and examples, if you plan on on attempting such an application in AppleScriptObjC, you would have to convert any suitable tutorials Swift or ObjectiveC code into  AppleScriptObjC code, which is something people here might be able to help with.

So it is not as simple as merely placing your NSStatusBar code into an NSTouchBar application, there very different types of app.
I personally wouldn't attempt an NSTouchBar application from the Script Editor, that's not to say it couldn't be done, but testing and debugging such an app project would be a nightmare.
And is something I personally would only undertake from Xcode, which in itself has a big learning curve.
Also who ever was going to be able to help with with coding such an app, that person would need a Apple laptop with a Touch Bar, which I don't have, but I'm sure many others here do have.

But for the moment, Apple has some documentation related to the NSTouchBar class. … nstouchbar … _touch_bar
These pages will give you some idea of what's involved with such an application.
And you will find many other tutorials online as well, which will also inform you as to what's involved.

Regards Mark

Last edited by Mark FX (2021-12-06 10:53:49 am)



#4 2021-12-06 01:16:31 pm

Registered: 2021-11-15
Posts: 17

Re: write text on Touch Bar

Thanks @Mark FX. Let me look at the documents.



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)