Send text to non-active window via autohotkey - export

For my project I need a program that exports text or spreadsheet data to other programs like word, excel, notepad, etc..
The user will set his cursor in the application he wants to export to, then he clicks in my program on "export", which will start the autohotkey .exe. I already figured out how to get the window of the other application, but not how to send the data.
Here is the current code:
WinGet, id, list
window_id := id3 ;id2 = current program, id3 = program behind (we want to send the data here)
ControlSendRaw, , MyExportDataHere, ahk_id %window_id% ;<-------- DOESNT WORK
;~ ///////////// Works, but ugly because of popup:
;~ window_id := id3
;~ WinActivate, ahk_id %window_id%
;~ SendRaw MyExportDataHere
;~ window_id := id2
;~ WinActivate, ahk_id %window_id%

Use the first parameter of ControlSendRaw to specify the Control like this:
http://lexikos.github.io/v2/docs/commands/ControlSend.htm#Function_Syntax

Related

How to distinguish between iTerm2 windows to move / resize only one of them with Hammerspoon?

I have two iTerm2 windows open, and I want to place and resize one via Hammerspoon while leaving the other one as is.
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "Right", function()
local layout = {
{"iTerm2", "/in/my/work/dir", nil, hs.geometry.rect(0.0, 0.40, 0.6, 0.6), nil, nil},
}
hs.layout.apply(layout)
end)
When I run the code as above, nothing happens; Hammerspoon does not find my iTerm2 windows, even though it looks like my window has "/in/my/work/dir" in the title.
When I replace "/in/my/work/dir" with nil, Hammerspoon moves and resizes all my iTerm2 windows. This makes sense, since I just ask for all iTerm2 windows without specifying a title.
When I add debug code to print the list of my iTerm2 windows to the console, I see only one iTerm2 window:
hs.fnutils.each(hs.application.runningApplications(), function(app)
if string.find(app:name(), "iTerm2") then
print("Found " .. app:name() .. " -- " .. app:title())
end
end)
This prints:
Found iTerm2 -- iTerm2
How can I use Hammerspoon to tell my iTerm2 windows apart and move / resize only one of them?
This actually works when you pass the complete title, not only a part of the title. I guess I got misled when the documentation said "string containing a window title". It has to be an exact match.

Find and replace in text files using AppleScript

I am trying to write an applescript which will run via a launch agent. What the script needs to do is edit a user preference plist file so that default save locations are specific to that user. I am aware that this can be done by just setting "~/documents" as the location in the template plist. But Premier Pro for example also needs to write scratch files to a local drive. For simplicity I would like each user to have these put in a locations based on their username. This script will only need to run if the local profile has just been created from a template at first log on.
I have started by using some sample code found on this site and just making a simple test below. This test should edit a txt file and replace one word with another. This script is currently not working. When tested it opens up test.txt in TextEdit but does nothing more. No errors are displayed either.
Thank you in advance
John.
replaceText("replace this", "replace with this", "/Volumes/USB_Drive/test.txt")
on replaceText(search_string, replacement_text, this_document)
tell application "TextEdit"
open this_document
set AppleScript's text item delimiters to the search_string
set this_text to the text of the front document as list
set AppleScript's text item delimiters to the replacement_text
set the text of the front document to (this_text as string)
close this_document saving yes
end tell
end replaceText
Here an version that doesn't need text edit. It will read the file in utf-8 encoding, update it's contents and store that back into the file as utf-8 encoded text. The reason I use an try block around writing the file is that there will be an error if another application has the file open with read permission at the same time. The considering case block can be wrapped around the set ti to every text item of theContent if you want the search and replace case sensitive. There is no need for this to be active when you replace the string, only for finding it.
set stringToFind to "replace that"
set stringToReplace to "with this"
set theFile to choose file
set theContent to read theFile as «class utf8»
set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, stringToFind}
set ti to every text item of theContent
set AppleScript's text item delimiters to stringToReplace
set newContent to ti as string
set AppleScript's text item delimiters to oldTID
try
set fd to open for access theFile with write permission
set eof of fd to 0
write newContent to fd as «class utf8»
close access fd
on error
close access theFile
end try
Well, yes, as #dj_bazzie_wazzie points out, you really don't need a text editor for this, you can use the terminal and do something like:
perl -pi -e 's/old text/new text/g' /path/to/theFile.plist
which of course you can use in AppleScript with the powerful do shell script command:
do shell script "perl -pi -e 's/" & search_string & "/" & replacement_text & "/g' " & quoted form of (POSIX path of file_path)
--assuming file_path is a variable with Mac-style (colon-separated) file path.
Modified from http://discussions.apple.com/message/20542594#20542594, the handler below does what you want. I made a few changes and added your variables. A few notes: (1) the 'my' before the handler call makes sure it is seen as the script's handler and not something TextEdit should interpret 'internally' (because it is in a tell block); (2) 'considering case' makes the handler case sensitive, which I assume you want; (3) You might consider something like TextWrangler, which has robust and feature-rich AppleScript support, and includes text replacement in its AS dictionary, as does Smile, a fantastic Script Editor (which can work with text, and formats plist files nicely).
tell application "TextEdit"
set workingWin to open this_document
set this_text to the text of the workingWin
set the text of the workingWin to (my replaceText(this_text, search_string, replacement_text))
close workingWin saving yes
end tell
to replaceText(someText, oldItem, newItem)
(*
replace all occurances of oldItem with newItem
parameters - someText [text]: the text containing the item(s) to change
oldItem [text, list of text]: the item to be replaced
newItem [text]: the item to replace with
returns [text]: the text with the item(s) replaced
*)
considering case
set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, oldItem}
try
set {itemList, AppleScript's text item delimiters} to {text items of someText, newItem}
set {someText, AppleScript's text item delimiters} to {itemList as text, tempTID}
on error errorMessage number errorNumber -- oops
set AppleScript's text item delimiters to tempTID
error errorMessage number errorNumber -- pass it on
end try
end considering
return someText
end replaceText

Loop, Read stops after one record with GUI (doesn't loop)

I have a script for entering records in our system that was originally working fine with a MsgBox, but I added a GUI to show the record entry. Now the script stops after the first record.
In the example below I've stripped out all of the actions and record lines to help make this easier to parse, but I've kept in all the important stuff and tested this version of the script.
Loop, read, C:\_AutoHotKey\AA_test.txt
{
StringSplit, LineArray, A_LoopReadLine, %A_Tab%
aaduedate := LineArray1
aauniqueid := LineArray2
aaprefix := LineArray3
aasequence := LineArray4
aadescript := LineArray5
aaelig := LineArray6
;-------------------------------------------------------------------------------------
;Use these to test the file match in the Input File.
;Remove surrounding comments and surround the rest of the script up to the last brace.
SendInput, Prefix: %aaprefix% {enter}
SendInput, Sequence: %aasequence% {enter}
SendInput, Description: %aadescript% {enter}
SendInput, Eligibility: %aaelig% {enter}
SendInput, ID Card: %aaidcard% {enter}
;---------------------------------------------------------------------------------------
;Pop-up validation menu
Gui, Add, Button, x22 y380 w100 h30 , &Submit
Gui, Add, Button, x362 y380 w100 h30 , &Cancel
Gui, Font, S14 CDefault, Verdana
Gui, Add, Text, x152 y10 w210 h30 +Center, Is the entry correct?
Gui, Font, S10 CDefault, Verdana
Gui, Add, Text, x102 y40 w90 h20 , %aaprefix%
Gui, Add, Text, x102 y70 w130 h20 , %aaelig%
Gui, Add, Text, x312 y70 w30 h20 , %aadescript%
Gui, Add, Text, x432 y70 w30 h20 , %aaidcard%
Gui, Font, S8 CDefault, Verdana
Gui, Add, Text, x132 y380 w230 h40 +Center, Click Submit/press S to continue. Click cancel to stop script.
; Generated using SmartGUI Creator 4.0
Gui, Show, x9 y250 h428 w480, Auto Action Validation
Return
ButtonCancel:
ExitApp
ButtonSubmit:
Gui, Submit ;
MouseMove, 630,55
Sleep, 100
SendInput, {Click 630,55}
SendInput ^S
Return
}
The buttons do work and clicking Submit will send the MouseMove and SendInput. But after that it just stops and doesn't load the next record in the text file.
Thanks in advance!
You have a return command within the loop.
This exits the program
See this example.
This loop should run 5 times, but the return
command stops it after the first run.
Loop, 5
{
msgbox, %A_Index%
return
}
I was able to get this to work by removing the submit button from the GUI and moving it to a MsgBox.
The GUI is basically the same but the Submit and Cancel lines have been removed along with the returns and all logic following it.
Next, a MsgBox was added to confirm the data. This now uses the GUI to display the contents of the record and the MsgBox to confirm and move on to the next record.
Also, there's some code I stole (and don't really understand) that moves the MsgBox so that it doesn't block the data entry screen in the receiving app.
Here's the new code that replaces everything after Gui, Show, x9...
OnMessage(0x44, "WM_COMMNOTIFY")
MsgBox 4, AA Entry, Would you like to continue? (press Yes or No)
WM_COMMNOTIFY(wParam) {
if (wParam = 1027) { ; AHK_DIALOG
Process, Exist
DetectHiddenWindows, On
if WinExist("ahk_class #32770 ahk_pid " . ErrorLevel) {
WinGetPos,,, w
WinMove, 90, 650
}
}
}
;If the user responds yes, then close the Gui (Destroy) and enter the record
IfMsgBox Yes
{
Gui destroy
Sleep, 100
}
else
ExitApp
}
Thanks, and I hope this is helpful to someone.

Batch file for 1 mouse click

Hello just wondering is their any possibility for a batch file to have a user entry specified time? Lets say for example i want to enter a time of 26 minutes, after that the batch file will be like a countdown timer when it reaches 0 it does 1 mouse click to an icon on the desktop? or where to pointer of the mouse was pointed?
Try Autohotkey or AutoIT, it's very easy to do these thing with this... or you could just get an autoclicker...
Example
#Persistent
Pause
SetTimer, MouseClicklabel , 1560000 ; 1000 = 1 second, meaning 1560000 = 26 Minutes
Pause::Pause
MouseClicklabel:
Click
Return
The toggle key is Pause

how to select a file by default when open a folder in vfp 9.0

I want to open a folder, and select a file by default.
I do it like this:
Declare Long WinExec In kernel32 String #, Integer
WinExec("Explorer /select, C:\tt.txt",5)
But if the folder has been opened, the file can't be selected by default.
How to do it?
What is your purpose of prompting a user with picking a particular file...
The closest you can get from wthin VFP is "GetFile()" where you can give it a default extension of a file you are hoping to find and it brings up a file selection dialog.
lcFileSelected = GetFile( "Txt", "Caption left of combobox selection (but only shows about 16 chars)", "Button Caption", nOptionalButton )
where ex:
nOptionalButton
0 = no extra button at bottom right, just the OK, Cancel (where OK is overridden by the "Button Caption" sample above.
1 = OK, New, Cancel
2 = Ok, None, Cancel
If a value selected, you'll have the file name, otherwise blank.
REVISED ANSWER..
Then what you want is PUTFILE() which allows you to prompt a user a simple message, similar to a "Save to", and allows to put a fully qualified path and file name. Upon return, much like that of doing GETFILE() will return the final path/file name entered by the user. Ex:
lcUserAnswer = PUTFILE( "save where", "C:\program files\myTest.txt" )
now you can do whatever with the "lcUserAnswer" variable...

Resources