Setting path for AHK's FileSelectFile function not working - autohotkey

My goal is to create a script that will allow me to paste images into a social media platform's new post instance. This platform allows pasting images from Clipboard.
My logic is:
Open the File Select modal with a specific default path.
Select a file by user.
Pass the selected object to the Clipboard.
Send the Paste command back into the Post instance.
I was able to complete all of this; the only issue I'm having is setting the absolute starting path of the File Select modal; no matter what I do, or how I pass the absolute path, it seems to always open the path of the last physical action outside the script, in other words, if I physically opened a Explorer from Windows itself or any other app, it will use that path, instead of the one I mentioned in the script; it's been driving me nuts. I've rebooted the script, refreshed everything, cleared cache, did a rain dance.
Would love some assistance!
Here's my script:
^+x::
mypath := "C:\Users\MyComputerName\Downloads\SOCIAL-MEDIA\Images"
FileSelectFile, SelectedFile, 3, %mypath%
if (SelectedFile = "")
MsgBox, The user didn't select anything.
else
pToken := Gdip_Startup()
Gdip_SetBitmapToClipboard(pBitmap := Gdip_CreateBitmapFromFile(SelectedFile))
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)
send ^v
return

It seems that setting the third parameter to "2" aka "Path must exist," did the trick.
https://www.autohotkey.com/docs/v1/lib/FileSelectFile.htm

Related

Weird cases when drag and droping files in to vscode terminal

My problem
I am currently working with Python in VS Code and I encountered a weird problem with the drag and drop feature. When I drag a folder path or a file path into the Terminal, then it always begins with the &
sign. This only happens when I start my program.
Here is an example:
& 'filepath' or
& "filepath"
I don't have a problem with the quotes but with the & and the blankspace. I can't find anything online, I was wondering if anybody else has this problem.
Thanks in advance :)
What did you try and what were you expecting?
Well, I did try it with other files and folders, but it didn't help.
I've also tried running the code outside VS Code and drag and dropping was fine. So I guess it has something to do with VS Code.
This happens in certain conditions when the path contains a space or parenthesis and the current terminal is powershell based.
I'm not sure why this was added, but the mechanism happens here:
src/vs/workbench/contrib/terminal/browser/terminalInstance.ts:2664
if (isPowerShell && (hasSpace || originalPath.indexOf('\'') !== -1)) {
return `& '${originalPath.replace(/'/g, '\'\'')}'`;
}
if (hasParens && isPowerShell) {
return `& '${originalPath}'`;
}
The original PRs introducing this change are here and here
The rationale seems to be to run files with certain symbols in their name:
single quotes in paths for PowerShell (i.e /Users/tyler/tyler's stuff/ -> & '/Users/tyler/tyler''s stuff/' to escape a ' in a ' ' string, you add an extra ')
The smallest workaround is to not open a powershell terminal.
So either open a terminal with the drop-down next to the "+" (#command:workbench.action.terminal.newWithProfile) which should have other entries next to powershell like "Command Prompt"(cmd.exe).
If you want to make this the default select the terminal profile then use the "Terminal: Select Default Profile"
(#command:workbench.action.terminal.selectDefaultShell) command to change the default.
If you want this to be configurable or not happen when the foreground application is no longer powershell you should probably file an issue with vscode.

Task Scheduler | I am trying to schedule a .ps1 file but every time the scheduler runs a pop window appear "How Do you want to open this file?"

Here's a screenshot from scheduled task action settings
First of all make sure the path used for powershell is correct (best option in my opinion is to click Browse... and select the program with the dialog window instead of manually inserting the full path, if that's what you did).
In second place, change Add arguments (optional): to -File "D:\TM1 Model\Test GIIS\...\yourfilename.ps1", please note you should use double quote since the full path contains spaces; again, make sure the full file path is typed correctly.
There should be no need to insert the full path in the in the Program/script box. Most people don't even know where it is.
For all the PowerShell scripts that I run on servers I only type powershll.exe and it works.
I agree with Giacomo (Calvin?), the value in the Add Arguements box needs to be in quotes if you have spaces in the folder or file name. ie -File "C:\Batch Files\checkdisk.bat"
The -File argument is telling PowerShell the file you want powershell.exe to run.

Start App from file storing settings and load them at startup

I'm working on an App in app designer. Within the app the user will select a bunch of options before running some calculations.
To simplify this process I added a "Save as..." menu so that the user can save the current settings to a file (.mat) and reload them when they open the app the next time.
What I'm trying to achieve is that the user can double click on the previously saved .mat file, which will launch the app and the app will automatically read the double clicked file and load all the settings.
All this needs to happen after the app is compiled and distributed as an executable.
I'm thinking that one way to achieve this is to make a startup window of the app that calls the main window passing the file path as parameter.
Any suggestion would be really appreciated.
Hi, I think I may have a fairly simple, albeit involved, solution for you.
Brief solution overview (TL;DR)
Save the settings from the app with an extension other than .mat, e.g. .mydat. Add an App Input Argument and have the startupFcn treat the argument as a file name to a *.mydat file and be sure to also handle the case that the argument is left out. After the first output file is saved, use windows Open with... to select your app. Now double clicking the *.mydat file will open your app's .exe and will provide the file name of the clicked file to the input argument in your startupFcn.
An example in MATLAB 2018a as a compiled exe on windows 10.
Ok, to start. Let's setup a simple app (I called it runAppFromData) that takes a string input to an edit field and saves it in a file called 'settingsValues.mydat'. It looks like:
The callback for the Save button collects the Value into a local variable called value and then saves it to disk:
% Button pushed function: Save
function save(app, event)
value = app.InputField.Value;%#ok
% User selects save location
saveLocation = uigetdir();
% Now just save the value variable to the selected location.
save(fullfile(saveLocation,'settingsValues.mydat'), 'value', '-mat');
end
I don't know when appdesigner added the feature to "run app with inputs" but I have it with 2018a:
We make a single input, fileName that expects a file name as a string (you'll see why below). So add the input and click OK. Then we're sent to "code view" at the startupFcn. Here we'll write the logic that parses the input file. For my simple example app, I load the input file into a struct and then send the value to the edit field:
% Code that executes after component creation
function startupFcn(app, fileName)
if nargin < 2 % app is itself an argument
% just continue running the application without error
return
end
% fileName is a string, so let's load it into a struc
S = load(fileName, '-mat');
% The value field will be there because that is how we wrote it
app.InputField.Value = S.value;
end
Note, I performed a nargin check to handle the first-run case (and anytime the app is run from the actual executable).
MATLAB doesn't care what the file extension is of a matlab file and if you have an unknown file extension, e.g. .mydata, double-clicking the file in windows will ask you to choose the application, which works to your benefit for deployment:
A couple things to consider.
When the app is opened from the .exe it will always show the default values. If you want to input some other default values you can edit your windows shortcut Target field to supply a file path for the desired input file (see here). This saves recompiling with new defaults, but the file has to remain somewhere (you can package it with the app too).
Sorry this answer got soo long! I hope it helps!
You can't double click a .mat file and open an entire executable, but you can definitely add a startup function that asks you to open a .mat file. My suggestion though would be to make sure that you have a template file at least in place, so that the user doesn't run into problems the first time running the program where there is no file to open.

Passing the current File Path through to Powershell "SendTo" Script

I'm trying to create a script that will let me copy an item from one location to a specified location in a PowerShell script. Before it's mentioned, I know that I can put a shortcut in the Send To directory to perform this action, but I would like to add additional features (that's for a different time). I do apologize if there is another post relating to this, I've been looking for a day and a half to find one.
What I would like to know is if there is a way I can pass the current-item-that-I-am-right-clicking's file path to PowerShell to be used with the Copy-Item function. So if there is an item on the desktop, I can use this PowerShell script to Copy-Item C:\Users\USERNAME\Desktop\File.ext using the path as a variable (I believe that would be the appropriate term) from the "Send To" Selection in the context menu.
You don't have to do anything special to get the Send To context menu to send the file path to the target - it happens automatically.
Try the following:
Create a new script, let's call it C:\Copy-SendTo.ps1
param($SourceFile)
Copy-Item $SourceFile -Destination C:\your\specific\location\
Now, create a shortcut in $env:APPDATA\Microsoft\Windows\SendTo with the name "CopyTo" and the following target:
%windir%\System32\WindowsPowerShell\v1.0\powershell.exe C:\Copy-SendTo.ps1 -SourceFile
Now right-click a file on your desktop, select Send To > CopyTo.
Voila, your file has been copied

editing objects from MongoDB with an external editor doesn't update the object

I'm using the Mongo shell. I've set my EDITOR to my notepad++ path. I create an object and then I use the EDIT command to edit the obeject using notepad++ but it doesn't update the object.
// mongo shell
var pow = { name: "teest" };
edit pow
// notepad++ opens a document called 'mongo_edit141225123.js' that resides
// in C:\users\...\Appdata\local\temp
// I edit the object, save and close notepad++
pow // object isn't updated :(
what am I missing?
There seem to be a few caveats here. But I can describe how I got this working:
Set the PATH environment variable to include the path to the notepad++ executable. Note to both "apply" this change and not have an existing command line window when doing so. Or at least open a new one once this step is complete.
Specify an EDITOR variable in your command shell window, or otherwise set that under the same system properties as setting the PATH environment variable. Since the program directory is in the PATH just set the executable name:
set EDITOR="notepad++"
Launch your mongo shell and go to edit a variable:
> edit something
This will launch the specified editor, with an "undefined" variable at first. Type in something "valid", as any invalid JavaScript declaration will be discarded. Now for the important part. After your edit and when "closing" click the "tab close" icon and do not close the entire editor as shown:
That last part seems to be the most important. If you are prompted to save (and you likely will be ) then do so. Only "after" the tab has been closed (and saved) should you then close the editor itself.
If you then subsequently issue the same edit something from the mongo shell, then the editor will open with the content that you edited before.
If you don't follow this and just close the editor window first, then you should see an additional tab opened and the original tab with the content that you had before. But subsequent changes will be lost as the shell is now tracking a different temporary file.
So follow those steps and you should be right. I would expect there are similar issues with other external editors that will actually resolve in a similar way.