• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: replacing selected text in a document
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: replacing selected text in a document


  • Subject: Re: replacing selected text in a document
  • From: "Nigel Garvey" <email@hidden>
  • Date: Wed, 16 May 2007 12:30:35 +0100

David Rice wrote on Tue, 15 May 2007 20:43:21 -0400:

>That worked. But, if I may, why does this not work?
>on process(_str)
>	tell application "System Events"
>		set _appname to name of first process whose frontmost is true
>	end tell
>	tell application _appname
>		set _str to "[" & _str & "]"
>		keystroke _str
>	end tell
>end process

Hi, David.

'keystroke' is a command that's only understood by System Events (in
Tiger and, I think, Panther), so you have to 'tell' System Events to do
it. In the code above, you're giving the command to some other
application. In fact, since the name of that application isn't known by
the compiler, I'm slightly amazed that it actually compiles.

You may be thinking of a structure often used in GUI Scripting, where an
application _process_ is apparently told to 'keystroke':

  tell application "System Events"
    set _appname to name of first process whose frontmost is true
    tell application process _appname
      set _str to "[" & _str & "]"
      keystroke _str
    end tell
  end tell

This works because 'keystroke' is a System Events command and an
'application process' is a System Events element. (Not to be confused
with an 'application', which is an application in its own right!) But
it's System Events, rather than the process, that executes the command.

>And is there a way to paste into the front app. The clipboard is set
>to the appropriate string because after running the script I
>originally posted, doing a Command V pasted in the desired info.

'paste' is similarly a command that's only understood by certain
applications. But you can use System Events's 'keystroke' to simulate
Command-v, which is the manual equivalent in most apps:

  on process(_str)
    -- Same assumptions about TextEdit and selection....
    set _str to "[" & _str & "]"
    set the clipboard to _str
    tell application "System Events"
      keystroke "v" using {command down} -- "Paste" (Command-v.)
    end tell
  end process

Although 'keystroke' is often used in GUI Scripting, it also (currently)
works when GUI Scripting isn't enabled.

NG

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >Re: replacing selected text in a document (From: "Nigel Garvey" <email@hidden>)
 >Re: replacing selected text in a document (From: David Rice <email@hidden>)

  • Prev by Date: Re: Event listener plug-in for Finder - does such a thing exist?
  • Next by Date: Re: Appending text?
  • Previous by thread: Re: replacing selected text in a document
  • Next by thread: Appending text?
  • Index(es):
    • Date
    • Thread