If Statement?

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

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!

Maybe this is what you need:

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

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!

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

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.

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 :

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

and use :


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

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.

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

and

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

appear to return the same response.

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

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

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:

Or this:

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:

Aha! :slight_smile:

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

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:

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.

 -- 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

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

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

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

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|()}