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…
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.
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…
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…
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.
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: