Sunday, May 9, 2021

#1 2021-04-25 07:53:11 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 871

Character ID Script

I occasionally need to know the ID or description of a character in a document, and I wrote the following script for this purpose. A few comments:

* This script can be tested by opening it in Script Editor, selecting one or more characters, and running the script.

* Some characters are not visible or may be difficult to recognize, and in these circumstances the script provides a description of the character.

* When multiple characters are selected, the script reports the ID of the leftmost-selected character, although there is an option to show the ID of all selected characters.

* The character descriptions are based on what I believe to be commonly-accepted usage, rather than any recognized standard.

* The script appears to work with most applications that have an Edit > Copy menu item, although I have encountered an occasional odd result with Safari, which appears due to HTML formatting.

* I run this script by way of FastScripts but there are many other options.

Applescript:

-- Revised 2021.04.27

on main()
   set maxSelectionAllowed to 200
   set maxCharactersDisplayed to 30
   set characterData to {{0, "null"}, {9, "horizontal tab"}, {10, "line feed"}, {11, "vertical tab"}, {12, "form feed"}, {13, "carriage return"}, {32, "space"}, {34, "double quote"}, {39, "single quote"}, {45, "hyphen"}, {96, "backtick"}, {8194, "en space"}, {8195, "em space"}, {8211, "en dash"}, {8212, "em dash"}, {8216, "left single quote"}, {8217, "right single quote"}, {8220, "left double quote"}, {8221, "right double quote"}}
   
   set the clipboard to "No Text"
   tell application "System Events"
       set appName to (name of first process whose frontmost is true)
       tell process appName to click menu item "Copy" of menu "Edit" of menu bar 1
   end tell
   
   repeat 6 times
       set selectedText to the clipboard
       if selectedText ≠ "No Text" then exit repeat
       delay 0.05
   end repeat
   
   if selectedText = "No Text" then
       errorDialog("A character was not selected. The active application is " & appName & ".")
   else
       set theID to id of (text 1 of selectedText)
       set theDescription to quote & (text 1 of selectedText) & quote
   end if
   
   repeat with aList in characterData
       if (item 1 of aList) = theID then
           set theDescription to (item 2 of aList)
           exit repeat
       end if
   end repeat
   
   set selectedTextCount to (count selectedText)
   
   if selectedTextCount > 1 then
       display dialog "Application: " & appName & linefeed & "Character: " & theDescription & linefeed & "ID: " & theID buttons {"Show All", "OK"} cancel button 2 default button 2 with title "Character ID" with icon note
   else
       display dialog "Application: " & appName & linefeed & "Character: " & theDescription & linefeed & "ID: " & theID buttons {"OK"} cancel button 1 default button 1 with title "Character ID" with icon note
   end if
   
   if button returned of result = "Show All" then
       if selectedTextCount > maxSelectionAllowed then errorDialog("The selected string exceeds " & (maxSelectionAllowed as text) & " characters and cannot be displayed.")
       set text item delimiters to space
       set theIDs to (text items of (id of selectedText)) as text
       set text item delimiters to ""
       if selectedTextCount > maxCharactersDisplayed then
           set selectedText to "See selection in Application"
       else
           set selectedText to quote & selectedText & quote
       end if
       display dialog "Application: " & appName & linefeed & "String: " & selectedText & linefeed & "IDs: " & theIDs buttons {"OK"} cancel button 1 default button 1 with title "Character ID" with icon note
   end if
end main

on errorDialog(dialogText)
   display dialog dialogText buttons {"OK"} cancel button 1 default button 1 with title "Character ID" with icon stop
end errorDialog

main()

Last edited by peavine (2021-04-27 08:30:17 am)


2018 Mac mini - macOS Catalina

Offline

 

#2 2021-04-25 08:08:48 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 871

Re: Character ID Script

The following is a description of the "id" property of text objects from the AppleScript Language Guide. It provides some helpful background information, and special note should be made of the statement that a character can be composed of more than one code point, which can limit the usefulness of my script in certain circumstances.

A value (or list of values) representing the Unicode code point (or code points) for the character (or characters) in the text object. (A Unicode code point is a unique number that represents a character and allows it to be represented in an abstract way, independent of how it is rendered. A character in a text object may be composed of one or more code points.)

This property, added in AppleScript 2.0, can also be used as an address, which allows mapping between Unicode code point values and the characters at those code points. For example, id of "A" returns 65, and character id 65 returns "A".

The id of text longer than one code point is a list of integers, and vice versa: for example, id of "hello" returns {104, 101, 108, 108, 111}, and string id {104, 101, 108, 108, 111} returns "hello". (Because of a bug, text id ... does not work; you must use one of string, Unicode text, or character.)

These uses of the id property obsolete the older ASCII character and ASCII number commands, since, unlike those, they cover the full Unicode character range and will return the same results regardless of the user's language preferences.



https://developer.apple.com/library/arc … mentID_611

Last edited by peavine (2021-04-26 09:19:43 am)


2018 Mac mini - macOS Catalina

Offline

 

#3 2021-05-02 03:34:09 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1788

Re: Character ID Script

peavine wrote:

* The script appears to work with most applications that have an Edit > Copy menu item, although I have encountered an occasional odd result with Safari, which appears due to HTML formatting.

* I run this script by way of FastScripts but there are many other options.


To use your script without FastScripts, and in any application, including Safari.app, you can hide the script editor:

Applescript:


on main()
   set maxSelectionAllowed to 200
   set maxCharactersDisplayed to 30
   set characterData to {{0, "null"}, {9, "horizontal tab"}, {10, "line feed"}, {11, "vertical tab"}, {12, "form feed"}, {13, "carriage return"}, {32, "space"}, {34, "double quote"}, {39, "single quote"}, {45, "hyphen"}, {96, "backtick"}, {8194, "en space"}, {8195, "em space"}, {8211, "en dash"}, {8212, "em dash"}, {8216, "left single quote"}, {8217, "right single quote"}, {8220, "left double quote"}, {8221, "right double quote"}}
   
   set the clipboard to "No Text"
   tell application "System Events"
       set appName to name of first process whose frontmost is true
       if appName is in {"Script Editor", "Script Debugger"} then
           set visible of process appName to false
           set appName to name of first process whose frontmost is true
       end if
       tell process appName to click menu item "Copy" of menu "Edit" of menu bar 1
   end tell
   
   repeat 6 times
       set selectedText to the clipboard
       if selectedText ≠ "No Text" then exit repeat
       delay 0.05
   end repeat
   
   if selectedText = "No Text" then
       errorDialog("A character was not selected. The active application is " & appName & ".")
   else
       set theID to id of (text 1 of selectedText)
       set theDescription to quote & (text 1 of selectedText) & quote
   end if
   
   repeat with aList in characterData
       if (item 1 of aList) = theID then
           set theDescription to (item 2 of aList)
           exit repeat
       end if
   end repeat
   
   set selectedTextCount to (count selectedText)
   activate me
   if selectedTextCount > 1 then
       display dialog "Application: " & appName & linefeed & "Character: " & theDescription & linefeed & "ID: " & theID buttons {"Show All", "OK"} cancel button 2 default button 2 with title "Character ID" with icon note
   else
       display dialog "Application: " & appName & linefeed & "Character: " & theDescription & linefeed & "ID: " & theID buttons {"OK"} cancel button 1 default button 1 with title "Character ID" with icon note
   end if
   
   
   if button returned of result = "Show All" then
       if selectedTextCount > maxSelectionAllowed then errorDialog("The selected string exceeds " & (maxSelectionAllowed as text) & " characters and cannot be displayed.")
       set text item delimiters to space
       set theIDs to (text items of (id of selectedText)) as text
       set text item delimiters to ""
       if selectedTextCount > maxCharactersDisplayed then
           set selectedText to "See selection in Application"
       else
           set selectedText to quote & selectedText & quote
       end if
       display dialog "Application: " & appName & linefeed & "String: " & selectedText & linefeed & "IDs: " & theIDs buttons {"OK"} cancel button 1 default button 1 with title "Character ID" with icon note
   end if
end main

on errorDialog(dialogText)
   display dialog dialogText buttons {"OK"} cancel button 1 default button 1 with title "Character ID" with icon stop
end errorDialog

main()


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)