Friday, July 20, 2018

#1 2018-07-12 10:54:01 am

Surge87
Member
Registered: 2018-07-11
Posts: 5

If Statement?

I think I can do this by an if statement but not quite sure the syntax.  Right now I have this:

Applescript:

tell application "System Events" to tell process "Xxx"
tell (1st window whose value of attribute "AXMain" is true)
set windowTitle to value of attribute "AXTitle"
end tell
try
set cxName to value of static text 2 of splitter group 1 of splitter group 1 of window 1
on error number -1719
set cxName to "Unable to find Name"
end try
set the clipboard to (cxName)

end tell

set txt to the clipboard as text
set txt to my supprime(txt, "Advisor - ")
set txt_capped to titlecase (txt)
on titlecase (txt)
return do shell script "python -c \"import sys; print unicode(sys.argv[1], 'utf8').title().encode('utf8').strip()\" " & quoted form of txt
end titlecase

on supprime(t, d)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to ""
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end supprime

If it can't find the name in "static text 2 of splitter group 1 of splitter group 1 of window 1" it just prints "Unable to find Name" but I'd like it to then look in "static text 2 of splitter group 2 of splitter group 1 of window 1" as it moves here based on some conditions.  Any help or pointing in the right direction would be appreciated!

Offline

 

#2 2018-07-12 11:56:08 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3250

Re: If Statement?

Maybe this is what you need:

Applescript:

tell application "System Events" to tell process "Xxx"
   tell (1st window whose value of attribute "AXMain" is true)
       set windowTitle to value of attribute "AXTitle"
   end tell
   try
       set cxName to value of static text 2 of splitter group 1 of splitter group 1 of window 1
   on error number nbr
       if nbr = -1719 then -- If the 1st attempt returned the known error, try with the 2nd splitter group
           try
               set cxName to value of static text 2 of splitter group 2 of splitter group 1 of window 1
           on error number nbr
               -- The 2nd error wasn't the known one
               set cxName to "Got error # " & nbr
           end try
       else
           -- The first error wasn't the known one
           set cxName to "Got error # " & nbr
       end if
   end try
   set the clipboard to (cxName)
   
end tell

set txt to the clipboard as text
set txt to my supprime(txt, "Advisor - ")
set txt_capped to titlecase (txt)
on titlecase (txt)
   return do shell script "python -c \"import sys; print unicode(sys.argv[1], 'utf8').title().encode('utf8').strip()\" " & quoted form of txt
end titlecase

on supprime(t, d)
   local oTIDs, l
   set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
   set l to text items of t
   set AppleScript's text item delimiters to ""
   set t to l as text
   set AppleScript's text item delimiters to oTIDs
   return t
end supprime

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 12 juillet 2018 19:55:48

Last edited by Yvan Koenig (2018-07-13 02:50:42 am)

Offline

 

#3 2018-07-12 04:35:46 pm

Surge87
Member
Registered: 2018-07-11
Posts: 5

Re: If Statement?

Yvan Koenig wrote:

Maybe this is what you need:

Applescript:

tell application "System Events" to tell process "Xxx"
   tell (1st window whose value of attribute "AXMain" is true)
       set windowTitle to value of attribute "AXTitle"
   end tell
   try
       set cxName to value of static text 2 of splitter group 1 of splitter group 1 of window 1
   on error number -1719
       try
           set cxName to value of static text 2 of splitter group 2 of splitter group 1 of window 1
       on error
           set cxName to "Unable to find Name"
       end try
   end try
   set the clipboard to (cxName)
   
end tell

set txt to the clipboard as text
set txt to my supprime(txt, "Advisor - ")
set txt_capped to titlecase (txt)
on titlecase (txt)
   return do shell script "python -c \"import sys; print unicode(sys.argv[1], 'utf8').title().encode('utf8').strip()\" " & quoted form of txt
end titlecase

on supprime(t, d)
   local oTIDs, l
   set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
   set l to text items of t
   set AppleScript's text item delimiters to ""
   set t to l as text
   set AppleScript's text item delimiters to oTIDs
   return t
end supprime

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 12 juillet 2018 19:55:48


Thanks.  I'm testing this out.  It still got to "Unable to find Name" a few times but not sure why.  Overall it has been working correctly.  Thank you!

Offline

 

#4 2018-07-13 02:48:44 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3250

Re: If Statement?

I enhanced the script so it will tell you which error is issuing  "Unable to find Name".

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 13 juillet 2018 10:48:31

Offline

 

#5 2018-07-13 03:06:07 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4604

Re: If Statement?

Surge87 wrote:

Applescript:

on titlecase (txt)
   return do shell script "python -c \"import sys; print unicode(sys.argv[1], 'utf8').title().encode('utf8').strip()\" " & quoted form of txt
end titlecase


Hi.

Just out of interest, and not in connection with your question, do you have the Satimage OSAX installed on your machine?

The reason I ask is that Satimage has a 'titlecase' command which performs the same function as your handler, except that it doesn't strip leading and trailing spaces. On a machine with Satimage, 'titlecase' is compiled against the Satimage command and your handler becomes an intercept for calls to it, executing the 'do shell script' instead of the Satimage command. The fact that both your posted code and Yvan's have a space between 'titlecase' and its parameter suggests this is what's happening here.

On a machine without Satimage, the source code still compiles and works, but 'titlecase' is compiled as an ordinary handler label. In this form, the compiled script will also work on a Satimage-equipped machine. But to compile on a Satimage machine for use on a machine without, 'titlecase' should be changed to '|titlecase|' (vertical bar at each end) or something else entirely. In fact it may be better to do this anyway.


NG

Offline

 

#6 2018-07-13 08:02:03 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3250

Re: If Statement?

Good point Nigel.
I missed the extraneous space.

If Satimage is not available relying upon Python is time wasting.

For my own use I would add three instructions at the very beginning :

Applescript:

use AppleScript version "2.3.1"
use scripting additions
use framework "Foundation"

and use :

Applescript:


set txt_capped to my makeCapitalizedOf:txt

on makeCapitalizedOf:aString
   return (current application's NSString's stringWithString:aString)'s capitalizedString() as text
end makeCapitalizedOf:

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 13 juillet 2018 16:01:56

Offline

 

#7 2018-07-13 09:01:33 am

akim
Member
Registered: 2010-04-04
Posts: 77

Re: If Statement?

What does  the  addition of the coercion words, "as text" to the capitalizedString() property, yield differently  than capitalizedString() without the coercion?
It appears to me that the representation provided by the capitalizedString() property is already a string.

Applescript:

current application's NSString's stringWithString:aString's capitalizedString() as text

and

Applescript:

current application's NSString's stringWithString:aString's capitalizedString()

appear to return the same response.

Offline

 

#8 2018-07-13 09:09:15 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3250

Re: If Statement?

I guess that you use Script Debugger which displays the converted string.
But when putting the value in the clipboard we doesn't get a string but a NSString  «class ocid» id «data optr00000000F0E02500C0600000»

Trying to paste such object in a text editor fails.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 13 juillet 2018 17:08:57

Last edited by Yvan Koenig (2018-07-13 09:12:46 am)

Offline

 

#9 2018-07-13 12:48:43 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4604

Re: If Statement?

Yvan Koenig wrote:

If Satimage is not available relying upon Python is time wasting.

For my own use I would add three instructions at the very beginning :
[…]
and use :

Applescript:


set txt_capped to my makeCapitalizedOf:txt

on makeCapitalizedOf:aString
   return (current application's NSString's stringWithString:aString)'s capitalizedString() as text
end makeCapitalizedOf:


Surge87's Python code also strips leading and trailing spaces, so it might be this in ASObjC:

Applescript:

use AppleScript version "2.3.1"
use scripting additions
use framework "Foundation"

set txt_capped to my makeCapitalizedOf:" now is the time for all good men "

on makeCapitalizedOf:aString
   set aString to (current application's NSString's stringWithString:aString)'s capitalizedString()
   return (aString's stringByReplacingOccurrencesOfString:"^\\s++|\\s++$" withString:"" options:(current application's NSRegularExpressionSearch) range:{0, aString's |length|()}) as text
end makeCapitalizedOf:


NG

Offline

 

#10 2018-07-13 05:48:54 pm

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

Re: If Statement?

Nigel Garvey wrote:

so it might be this in ASObjC:



Or this:

Applescript:

use AppleScript version "2.3.1"
use scripting additions
use framework "Foundation"

set txt_capped to my makeCapitalizedOf:" now is the time for all good men "

on makeCapitalizedOf:aString
   set aString to (current application's NSString's stringWithString:aString)'s capitalizedString()
   return (aString's stringByTrimmingCharactersInSet:(current application's NSCharacterSet's whitespaceAndNewlineCharacterSet())) as text
end makeCapitalizedOf:


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

Offline

 

#11 2018-07-13 06:15:21 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4604

Re: If Statement?

Shane Stanley wrote:

Or this:


Aha!  smile


NG

Offline

 

#12 2018-07-14 05:25:34 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4604

Re: If Statement?

Once we're into using ASObjC, the zapping of "Advisor - ", currently done with TIDs, could be included here instead:

Applescript:

use AppleScript version "2.3.1"
use scripting additions
use framework "Foundation"

set txt_capped to my capNzap:" Advisor - now is the time for all good men "
--set txt_capped to my capNzap:" now is the time Advisor - for all good men "
--set txt_capped to my capNzap:" now is the time for all good men Advisor - "

on capNzap:aString
   -- Capitalise first to ensure the "Advisor - " match goes OK.
   set aString to (current application's NSString's stringWithString:aString)'s capitalizedString()
   -- Delete any instances of "Advisor - "
   set aString to aString's stringByReplacingOccurrencesOfString:"Advisor - " withString:""
   -- Delete any leading or trailing white space.
   set aString to aString's stringByTrimmingCharactersInSet:(current application's NSCharacterSet's whitespaceAndNewlineCharacterSet())
   -- Return what's left as AS text.
   return aString as text
end capNzap:

Last edited by Nigel Garvey (2018-07-14 05:26:55 am)


NG

Offline

 

#13 2018-07-14 11:48:48 am

akim
Member
Registered: 2010-04-04
Posts: 77

Re: If Statement?

The following line in the prior script requires a command to capitalize the text to ensure that "Advisor" matches the same capitalized word in the text to be searched.

Applescript:

-- Capitalise first to ensure the "Advisor - " match goes OK.
set aString to (current application's NSString's stringWithString:aString)'s capitalizedString()

What would be the Applescript syntax of Objective C, were the goals to both:
1. Substitute the word "Adviser with an empty string, or ""
2. Maintain the current case of the search text, without capitalizing it

Offline

 

#14 2018-07-14 12:58:03 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4604

Re: If Statement?

akim wrote:

The following line in the prior script requires a command to capitalize the text to ensure that "Advisor" matches the same capitalized word in the text to be searched.


It's actually a handler to capitalize the string anyway. The removal of any instance of "Advisor - " and/or of leading or trailing space is in addition to that.  smile

But it is possible to do the substitutions without capitalising the text. Change this …

Applescript:

-- Capitalise first to ensure the "Advisor - " match goes OK.
set aString to (current application's NSString's stringWithString:aString)'s capitalizedString()
-- Delete any instances of "Advisor - "
set aString to aString's stringByReplacingOccurrencesOfString:"Advisor - " withString:""

… to this …

Applescript:

set aString to (current application's NSString's stringWithString:aString)
-- Case insensitively delete any instances of "Advisor - "
set aString to aString's stringByReplacingOccurrencesOfString:"Advisor - " withString:"" options:(current application's NSCaseInsensitiveSearch) range:{0, aString's |length|()}


NG

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)