Page 1 of 1

Process "ANY" text!

Posted: Tue Nov 25, 2025 6:14 pm
by Piero
Rough example………
Does not work on scintilla…

You will have to 1st create a Quick Action in Automator, named "writesel", this way:
Image
With this AppleScript code:

Code: Select all

property selt : "[[seltext]].txt"

on run {input}
	set this_file to ((path to documents folder from user domain) as text) & selt
	my write_to_file(input as text, this_file, false)
end run

on write_to_file(this_data, target_file, append_data)
	try
		set the target_file to the target_file as string
		set the open_target_file to open for access file target_file with write permission
		if append_data is false then set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file
I made it save to documents folder, to avoid (most) permission problems…

Then you can run this:
Be sure to grant permissions! (the 1st time it may not work…)

Code: Select all

EnableExplicit
Global ShOut.s
#NSSquareStatusBarItemLength = -2
Define ItemLength.CGFloat = 32
Define StatusBar,StatusItem
Define typet.s

Procedure Shell(ShellCommand$)
   Protected Output$, shell
   shell = RunProgram("/bin/sh","","",
      #PB_Program_Open|#PB_Program_Write|#PB_Program_Read|#PB_Program_Error )
   If shell
      WriteProgramStringN(shell,ShellCommand$)
      WriteProgramData(shell,#PB_Program_Eof,0)
      While ProgramRunning(shell)
         If AvailableProgramOutput(shell)
            Output$ + ReadProgramString(shell)
         EndIf
      Wend
   EndIf
   ShOut = Output$
EndProcedure

Define getseltxt.s = ~"osascript\n"+
   ~"property selt : \"[[seltext]].txt\"\n"+
   ~"set sel to \"\"\n"+
   ~"try\n"+
   ~"try\n"+
   ~"do shell script \"rm -f \" & quoted form of POSIX path of alias (((path to documents folder from user domain) as text) & selt)\n"+
   ~"end try\n"+
   ~"tell application \"System Events\"\n"+
   ~"set p to (1st application process whose frontmost is true)\n"+
   ~"perform action \"AXPick\" of menu item \"writesel\" of menu 1 of menu item \"Services\" of menu 1 of menu bar item 2 of menu bar 1 of p\n"+
   ~"repeat 10 times\n"+
   ~"delay 0.2\n"+
   ~"try\n"+
   ~"set sel to read alias (((path to documents folder from user domain) as text) & selt)\n"+
   ~"exit repeat\n"+
   ~"end try\n"+
   ~"end repeat\n"+
   ~"sel\n"+
   ~"end tell\n"+
   ~"on error e\n"+
   ~"sel\n"+
   ~"end try"

OpenWindow(0, 270, 100, 300, 100, "SysTray Menu Example",#PB_Window_Invisible)
UsePNGImageDecoder()
If LoadImage(0, #PB_Compiler_Home + "Examples/Sources/Data/World.png")
   StatusBar = CocoaMessage(0, 0, "NSStatusBar systemStatusBar")
   If StatusBar
      StatusItem = CocoaMessage(0, CocoaMessage(0, StatusBar,
         "statusItemWithLength:", #NSSquareStatusBarItemLength), "retain")
      If StatusItem
         CocoaMessage(0, StatusItem, "setLength:@", @ItemLength)
         CocoaMessage(0, StatusItem, "setImage:", ImageID(0))
         CreatePopupMenu(0)
         MenuItem(0, "Uppercase Text")
         MenuItem(1, "Debug user name")
         MenuItem(2, "Quit")
         CocoaMessage(0, StatusItem, "setMenu:", MenuID(0))
         Repeat
            Select WaitWindowEvent()
               Case #PB_Event_CloseWindow
                  Break
               Case #PB_Event_Menu
                  Select EventMenu()
                     Case 0
                        Shell(getseltxt)
                        if shOut
                           typet = UCase(shOut)
                           Shell(~"osascript\ntell application \"System Events\" to keystroke \""+EscapeString(typet)+~"\"")
                        EndIf
                     Case 1 : Debug "User name: " + UserName()
                     Case 2 : Break
                  EndSelect
            EndSelect
         ForEver
      EndIf
   EndIf
EndIf
Have Fun!

Re: Process "ANY" text!

Posted: Tue Nov 25, 2025 6:43 pm
by mk-soft
I don't know why you always call a shell for scripts.
You can use AppleScript directly from PB.

Link: AppleScript with ErrorInfo

Re: Process "ANY" text!

Posted: Tue Nov 25, 2025 7:09 pm
by Piero
mk-soft wrote: Tue Nov 25, 2025 6:43 pm I don't know why you always call a shell for scripts.
Because it's extremely easier, for many reasons…
…and you use cocoa, while I use shell… (we do not use PB!) :P

Re: Process "ANY" text!

Posted: Tue Nov 25, 2025 7:46 pm
by Piero
mk-soft wrote: Tue Nov 25, 2025 6:43 pmdirectly from PB.
I'm using an intermediate disk file to get the selected text of almost any app, NOT to run (complex) applescripts!
Try to get it (the selected text of almost any app) with cocoa! (I found nothing on the net, so this seems to be a good new trick) :D

Re: Process "ANY" text!

Posted: Tue Nov 25, 2025 10:30 pm
by Piero
Updated

Code: Select all

Shell(~"osascript\ntell application \"System Events\" to keystroke \""+EscapeString(typet)+~"\"")
:wink: