Get "ANY" Text Selection
Posted: Thu Dec 11, 2025 12:33 pm
Code: Select all
Procedure.s Shell(ShellCommand$)
Protected Output$, shell, tmper$
shell = RunProgram("/bin/sh","","",
#PB_Program_Open|#PB_Program_Write|#PB_Program_Read)
If shell
WriteProgramStringN(shell,ShellCommand$)
WriteProgramData(shell,#PB_Program_Eof,0)
While ProgramRunning(shell)
If AvailableProgramOutput(shell)
Output$ + ReadProgramString(shell)+ Chr(10)
EndIf
Wend
EndIf
CloseProgram(shell)
ProcedureReturn Left(Output$,Len(Output$)-1)
EndProcedure
Procedure HideMe(h=#False) ; default: hide (visible=false)
Shell(~"osascript\ntell application \"System Events\" to set visible of application process \""+GetFilePart(ProgramFilename())+~"\" to "+h)
EndProcedure
Procedure.s GetFrontmost()
ProcedureReturn Shell(~"osascript\ntell application \"System Events\" to name of (1st application process whose frontmost is true)")
EndProcedure
Procedure.s GetText(app.s)
ProcedureReturn Shell(~"osascript\n"+
~"tell application \"System Events\"\n"+
~"if (count of (application processes whose name is \""+app+~"\")) = 0 then return \"\"\n"+
~"set r to a reference to window 1 of (1st application process whose name is \""+app+~"\")\n"+
~"repeat\n"+
~"copy (value of attribute \"AXSelectedText\" of (UI elements of r whose focused is true) as string) to t\n"+
~"copy (a reference to UI elements of r) to r\n"+
~"if t is not \"\" or (count r) is 0 then exit repeat\n"+
~"end repeat\n"+
~"t\n"+
~"end tell")
EndProcedure
HideMe()
Shell(~"osascript\ntell app \"TextEdit\" to activate")
Shell(~"osascript\ntell app \"PureBasic\" to activate")
f.s = GetFrontmost()
Debug ~"Frontmost App ("+f+~") Selection:\n\n\""+ GetText(f) +~"\"\n\n"
Debug ~"TextEdit Selection:\n\n\""+ GetText("TextEdit") +~"\""