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