Thursday, February 25, 2021

#1 2020-09-06 03:20:32 pm

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

Apple Dictionary, SwiftCode to ASOC...(help needed)

I'm trying to convert this Swift code to ASOC, it complain about nil/NULL pointer...
I try missing value, 0, nil, null, NSNull... noting is working...

I also used CFRangeMake not sure if that is correct.

I believe its the CFString that I need but I use NSString... not sure if there is a solution.

The code should take a string check Apple Dictionary and return string when its working... smile

EDIT:
Everything start with a idea to convert a python script to ASOC. That was not possible from
the bridge of AppleScriptObjC. In this journey I discover its was possible with Automator with
ASOC to use Apple's Dictionary. But its also possible to use Swift, {check code exchange}.
Its also possible to use JavaScript to use Dictionary Service. I have not try ruby but I guess
it could be possible. If Dictionary Service or other frameworks is not possible in AppleScriptObjC
and you do not like to build a framework to be able to use it. It could be that Python, JavaScript
and Swift could be your solution.



Applescript:

use framework "Foundation"
use framework "CoreServices"
use scripting additions

(** Swift
* func define(_ word: String) -> String? {
* let nsstring = word as NSString
* let cfrange = CFRange(location: 0, length: nsstring.length)
*
* guard let definition = DCSCopyTextDefinition(nil, nsstring, cfrange) else {
* return nil
* }
*
* return String(definition.takeUnretainedValue())
* }
*)


my defineWord:"apple"

on defineWord:theString
   set aString to current application's NSString's stringWithString:theString
   set theCFRange to current application's CFRangeMake(0, aString's |length|())
   set theDefinition to current application's DCSCopyTextDefinition(missing value, aString, theCFRange)
   return theDefinition as text
end defineWord:

In mean time... python and properly javascript could maybe do it to...

#!/usr/bin/python

import sys
from DictionaryServices import *

def main():
    try:
        searchword = sys.argv[1].decode('utf-8')
    except IndexError:
        errmsg = 'You did not enter any terms to look up in the Dictionary.'
        print errmsg
        sys.exit()
    wordrange = (0, len(searchword))
    dictresult = DCSCopyTextDefinition(None, searchword, wordrange)
    if not dictresult:
        errmsg = "'%s' not found in Dictionary." % (searchword)
        print errmsg.encode('utf-8')
    else:
        print dictresult.encode('utf-8')

if __name__ == '__main__':
    main()



After Shanes respond and I was afraid it was the case.

But...

If you copy the python code and save it ex. Desktop/define.py and do chmod +x define.py
You could run this AS Script that use do shell script...

Applescript:

set theWord to "apple"

set thePath to POSIX path of (path to desktop as text) & "define.py"
do shell script "echo | " & quoted form of thePath & space & quoted form of theWord

This version only take the first dictionary and maybe the correct one.
To test this I have used 'American English Dictionary'
EDIT: Updated version to take 1 or more strings as searchStrings.

Applescript:

set theWord to "lemon juice"

set thePath to POSIX path of (path to desktop as text)
set theApp to "define.py"
set theText to do shell script "echo | " & quoted form of thePath & theApp & space & quoted form of theWord

if ((count words of theWord) > 1) then
   display dialog theText as text
else
   set resultText to my stringWithBeginEndString:theText beginWith:"1" endWith:"."
   display dialog (resultText & ".") as text
end if

on stringWithBeginEndString:inputString beginWith:beginString endWith:endString
   set savedDelimiters to AppleScript's text item delimiters
   set AppleScript's text item delimiters to beginString
   set foundString to item 2 of every text item of inputString
   set AppleScript's text item delimiters to endString
   set foundString to item 1 of every text item of foundString
   set AppleScript's text item delimiters to savedDelimiters
   return foundString
end stringWithBeginEndString:beginWith:endWith:

Last edited by Fredrik71 (2020-09-29 06:41:55 am)


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

Offline

 

#2 2020-09-06 05:41:18 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6565

Re: Apple Dictionary, SwiftCode to ASOC...(help needed)

ASObjC does not support the use of CFRef-style pointers, such as CFString.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2020-09-07 10:33:59 pm

technomorph
Member
Registered: 2017-12-14
Posts: 178

Re: Apple Dictionary, SwiftCode to ASOC...(help needed)

Try NSMakeRange.

Here’s some great info:

https://nshipster.com/nsrange/

You also may want to look into NSScanner

https://nshipster.com/nsscanner/

Offline

 

#4 2020-09-13 09:20:31 am

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

Re: Apple Dictionary, SwiftCode to ASOC...(help needed)

Lets say you still like to run AppleScript and be able to ask for words in dictionary
but you do not like to use Python of some reason. I thought about one way to do it and once
again Shane was the inspiration.

With Automator there is a action 'Get Definition of Word'

We will create a workflow that only have that action and save it to Desktop.

Now we could run this AppleScript ASOC

1. It use handler that run Automator workflow. (Shane Stanley)
2. It runs in main Thread.
3. The result is converted to string.
4. We ask for begin string and end string for result text.

Applescript:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "Automator"
use scripting additions

property theResult : missing value
property theError : missing value

set theInput to (display dialog "" default answer "apple")'s text returned

set thePath to POSIX path of (path to desktop) & "dict.workflow"
set theURL to current application's NSURL's fileURLWithPath:thePath
my performSelectorOnMainThread:"runWorkflow:" withObject:{theURL, theInput} waitUntilDone:true
if theError is not missing value then error theError's localizedDescription() as text

set theText to my arrayToString:(theResult as list)
set resultText to my stringWithBeginEndString:(theText as text) beginWith:1 endWith:"."

-- The object to run on main Thread.
on runWorkflow:theObject
   set {theURL, theInput} to theObject as list
   set {my theResult, my theError} to current application's AMWorkflow's runWorkflowAtURL:(theURL) withInput:theInput |error|:(reference)
end runWorkflow:

on arrayToString:theList
   return (current application's NSArray's arrayWithArray:theList)'s componentsJoinedByString:" "
end arrayToString:

on stringWithBeginEndString:inputString beginWith:beginString endWith:endString
   set savedDelimiters to AppleScript's text item delimiters
   set AppleScript's text item delimiters to beginString
   set foundString to item 2 of every text item of inputString
   set AppleScript's text item delimiters to endString
   set foundString to item 1 of every text item of foundString
   set AppleScript's text item delimiters to savedDelimiters
   return foundString
end stringWithBeginEndString:beginWith:endWith:


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

Offline

 

#5 2020-09-22 08:04:30 pm

technomorph
Member
Registered: 2017-12-14
Posts: 178

Re: Apple Dictionary, SwiftCode to ASOC...(help needed)

Ah this post answers my question in the other post about your CFString use.

You may want to look at this from Matt at NSHipster.

https://github.com/mattt/DictionaryKit

You can see how he uses bridging to get NSString from The CFString.


————————

So the code your looking for exsists.
You just may now have to learn to wrap into a framework.
And then learn how to use this framework in ASObjC.

I had to do this when I was wanting to integrate MIDI in my AppleScript.
It took a little bit of figuring out, but it was a great learning experience.

Offline

 

#6 2020-09-23 02:40:45 am

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

Re: Apple Dictionary, SwiftCode to ASOC...(help needed)

I will check it out... thanks.


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

Offline

 

#7 2020-09-23 04:54:12 am

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

Re: Apple Dictionary, SwiftCode to ASOC...(help needed)

technomorph wrote:

I had to do this when I was wanting to integrate MIDI in my AppleScript


In your example and your knowledge you choose to do it yourself and that is wonderful.

---

I have also used MIDI with AppleScript or in other words execute commands on remote device
like iPhone, iPad, Midi controller to execute command in AppleScript. Its far more faster to do
and use other technology I have tested.

Midi is a technology their latency is very important. It never ask if the package of bytes have been
revived or not. But it could send information is both directions.

And with Open Sound Control its possible to build interface on a device to control Apple Events.
In other words any device that connect to http server could execute AppleScript. I have done it
and its very powerful.

But its also possible to connect Midi controller of touch device directly to a computer or network
and send midi message. And execute command in AppleScript.

Script Menu is powerful, but it doesn't have to be the only way to execute commands,
when touch device will be the Script menu of buttons or other controllers to send commands with Midi, OSC to a computer that translate the message to Apple Event.

Other aspect.
Compare to Sal Soghoian demonstration of Accessibility panel that use Wifi its much faster.


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

Offline

 

#8 2020-09-29 01:52:11 am

technomorph
Member
Registered: 2017-12-14
Posts: 178

Re: Apple Dictionary, SwiftCode to ASOC...(help needed)

I love OSC and it’s simplification.  But not all
Apps speaks OSC.  I do a lot of multi-app integration with audio / video
And found having to use intermediate helpers like Osculator.

Check out VVOsc framework. And other VV stuff from the creator of VDMX software.
https://github.com/mrRay/vvopensource

In my case I was trying to get the Title/Artist info from a newly loaded deck in Traktor.
Some folks had found out that Traktor will send this info out via MIDI when you pretend that a Denon controller is hooked up.  It does this in a old 8bit LEF style scrolling text mode.
  Some one wrote some Java code to do this.  And IWant to get this all into AppleScript.

So i had to get the MIDI data, filter it. 
Learn how to send it to Java Code from AppleScript.   
Then learn how to send it back to AppleScript when it was “done”

Lots of new learning. And

Offline

 

#9 2020-09-29 06:20:17 am

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

Re: Apple Dictionary, SwiftCode to ASOC...(help needed)

My experience with Java have not been good, in many cases it take to much CPU for the task
it will do. I have test with python and JavaScript.

Open Stage Control use JavaScript and also python.
Its a application on top of Electron Framework and use ReactiveCocoa Framework.
I have use this application to trigger message from OSC to Midi to execute AppleScript.
The latency is very low... that why I find it very useful.

I'm a Ableton user and I have midi controllers keyboards but it could also be used as remote.

I have test button triggers, sliders and other things...
I have resize text in TextEdit with my midi keyboard nobs and that is fun... haha

I have thought, why build a interface in Interface builder or Qt to input values to something.
When we could do it with html, css and javascript. In some way OSC make this easier.
I only have to build the interface and translate the message to useful message.

If you like me to test anything you do, send me a message to my inbox.

And maybe its possible to use ReactiveCocoa Framework in ASOC.
From the header from ReactiveCocoa Framework I see it has AppKitBinding.

regards.


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

Offline

 

#10 2020-10-04 04:24:38 pm

technomorph
Member
Registered: 2017-12-14
Posts: 178

Re: Apple Dictionary, SwiftCode to ASOC...(help needed)

Many “suggestions” I’ve found. 
And especially from Shane Stanley who always offers a great
“Learning” perspective to his guidance is to
USE WHAT YOU KNOW.
For myself I don’t have much background in CSS or HTML
So for me building UI with those tools doesn’t make sense to me!
Though I can see the advantages of it in many places, I’ve just not had
The real need to do so! 
Many of my programming needs have pushed me into mainly
Programming in Objective-C.  And I did right when it seemed Swift was taking  off.
And many examples I’m finding are in Swift.  But I’ve not fully needed to yet
So I’m sticking with Objective-C.  Also many multithreading and speed differences
From my AppleScript versions were some reasons for me to shift.  Parsing some XML in
AppleScript that would take 12mins happens in way less time in ObjC for me now.

Thanks for your insights and while I’ve not needed MIDI and OSC stuff lately.
When it comes back I’m sure we’ll discuss here!!

Cheers

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)