ipython: showing CWD in prompt - ipython

In ipython, I have changed the prompt to show hostname and current working directory, to emulate system shell (bash):
c.PromptManager.in_template = '\\h:\\w '
this works well, except for a small detail. When I am in my home (/home/martin) it shows the full path, instead of the customary ~:
alpha:/home/martin
instead of
alpha:~
is it possible to change to ~ ?

you can use
c.PromptManager.in_template = '\\h:\\Xn '
where $n=0 .. 5.$ The current working directory, with $HOME replaced by ~, and filtered out to contain only $n$ path elements
so for example, you can use:
c.PromptManager.in_template = '\\h:\\X5 '

Related

AutoHotKey - How to take a screenshot and to paste it to a *.jpg file?

How to take a screenshot in Windows 8 and to paste it to a *.jpg file using AutoHotKey script? I want my custom key combination and folder for images
{CustomKey}:: ;image saved in Pictures/Screenshots folder by default
Send #{PrintScreen}
Return
The images are saved as png; can then be converted to jpg and moved to another directory.
Tested in Win 10.
Might work in Win 8 too.
Another question similar asked here:
https://autohotkey.com/board/topic/63742-how-to-save-a-screen-shot-with-ahk/
Problem was that saved file was blank. In my case, the file turned out to be all grey.
Reason was that FileAppend saved as text file.
To save screenshot image taken with PrintScrn button:
; Send {PrintScreen}
FileAppend %ClipboardAll%, FileName.raw, UTF-8
; This can be read back to memory with:
FileRead, Clipboard, *c FileName.raw
; image can also be converted/compressed to save space.
; I already had ffmpeg, so put this:
Run %ComSpec% /c "ffmpeg.exe -f rawvideo -pixel_format rgb32 -s 2256x1504 -i FileName.raw -vf hflip -vf vflip output.png"
Tested on Win 10.
You might try using the Gdip_All.ahk library. I found a maintained version of it at: https://github.com/mmikeww/AHKv2-Gdip/blob/master/Gdip_All.ahk
If you have Irfan View installed you can use this:
run('"C:\Program Files\IrfanView\i_view64.exe" /capture=0 "/convert=path_to_file.png" /jpgq=95')
You can also specify format or quality and include process name, time or other staff, for example:
dir := 'D:\screenshots'
name := winGetProcessName('A') ' ' a_YYYY '-' a_MM '-' a_DD ' ' a_hour '-' a_min '-' a_sec
format := 'png'
quality_jpeg := 95
run('"C:\Program Files\IrfanView\i_view64.exe" /capture=0 "/convert=' dir '\' name '.' format '" /jpgq=' quality_jpeg)
You can check Irfan View command line options in the program help or here.
You can also search some other programs that can silently make screenshots using command line options. If you find another good one let me know.

calling an external program in matlab in a loop

I have installed Matlab 2014 in Ubuntu. My problem:
I build several input files for another program, Quantum Espresso, in Matlab. Now I should pass these files to Quantum Espresso using matlab command line. Now I know I can do this using Linux Terminal, but my way of solving my problem has reached the point that my only option is 'calling Quantum Espresso from matlab'. One single call is actually easy:
! installation/folder/espresso-5.3.0/bin/pw.x < inputfile > outputfile
The problem is I have several input files named like 1name.in 1name.in ... . So this repeated calls should be done in a loop. But how?
I have tried:
the shell script for looping though the files. I added that extra '!' to each line of the script but it doesn't work.
I also tried to write a loop like this :
for i = 1:N
prefix = int2str(i);
fuloutname = [prefix 'name' '.' 'out'];
fulinname = [prefix 'name' '.' 'in'];
! adress/espresso-5.3.0/bin/pw.x < fulinname > fuloutname ;
end
In which 'N' in number of my input files. Clearly running this means you are passing a file nemaed 'fulinname' not 1name.in and will result in an output file named 'fuloutname'
I also tried to do it as you normally load various files in a loop but it also did not work
Please help me.
You should use the unix function:
for i = 1:N
prefix = int2str(i);
fuloutname = [prefix 'name' '.' 'out'];
fulinname = [prefix 'name' '.' 'in'];
mycommand = ['adress/espresso-5.3.0/bin/pw.x < ',fulinname,' > ',fuloutname];
unix(mycommand);
%system(mycommand); %will give you the same, result and this function is cross-platform
end

Can't make a directory using a variables value - Command Prompt

I'm trying to set up a simple backup process for a folder on my C drive that will back that folder up to another location on the network. I know how to create a scheduled task but I'm struggling to understand why my command prompt code won't work - I'm a novice when it comes to the Command Prompt though!
So my question is two fold:
Why does echo %variableName% not return the variable value - it only returns %variableName%.
This is what I type in:
#echo off
set varA = 5
echo %varA%
%varA% <- This is what its popping out
Do I need different preceeding and succeeding characters for this?
I want to create a folder with the date for the name (I do realize that are quite a few questions out there on this but they didn't work), how do I do it?
Here is what I tried:
set folder_name = %DATE:/=_%
set folder_name <- Display value for folder_name
folder_name = Wed 11_06_2013 <- Actual value
When I try to do this:
mkdir %folder_name%
dir
Creates a folder with this %_date% as the name.
Where am I going wrong?
Thanks
Space is significant in SET statements.
SET varspace=spacevalue
will set a variable named "varspace" to "spacevalue"
Remove the spaces and it should be plain sailing...
Oh - except that if the variable contains a space, then commands such as MD or mkdir (which are synonyms) require "rabbits ears" around the value, thus:
mkdir "%folder_name%"

Error writing image to file in Matlab

I am trying to write an image that I do operations on to a '.tif' file in a directory. I make the results directory with Matlab using the mkdir() function.
Here is the command I am using:
[pathstr, nameWOext, ext] = fileparts(filename);
results_dir = ['results' '/results_' nameWOext];
%check to see if the directory exists already, if it doesn't make it
if(exist(results_dir) ~= 7)
mkdir(results_dir);
end
filenamezero = [nameWOext '_J' ext];
imwrite (~J, fullfile(results_dir, filenamezero)); //Error here
When Matlab gets to this line it outputs an error:
Could not open file for writing. Check directory or file permissions.
I inspected the folder 'results/results_' and the folder is read-only. Apparently mkdir() is doing this automatically.
Is there anyway to get around this?
Thanks
P.S. I am running Windows 7 using Matlab 6.1
I think your problem may be your use of the fullfile function. I think the result is that the path you are trying to pass to imwrite has a mix of \ and / for file separators.
Try using this instead:
filenamezero = [nameWOext '_J' ext];
imwrite (~J, [results_dir '/' filenamezero]);
It seems that Matlab, when using an absolute path, requires to use ' / ' instead of the ' \ '.
For example, this works for me (Windows 8.1, Matlab R2012b)
imwrite(imagename, 'C:/Users/Myworkingfolder/myimage1.jpg','jpg');
But not:
imwrite(imagename, 'C:\Users\Myworkingfolder.jpg','jpg');
And this, even if Windows itself uses the ' \ ' when you copy a path from Windows explorer.
Although, when using a relative path, such as writing in the current folder in Matlab:
imwrite(imagename, 'Myworkingfolder/myimage1.jpg','jpg');
and
imwrite(imagename, 'Myworkingfolder\myimage1.jpg','jpg');
It works out of the box. It might be with how both cases (absolute and relative paths) are implemented...

Unicode named Folder shows ? in wscript prompt

I am facing problems with Unicode named folders. When I drag the folder to the script, it doesn't show the path of the folder properly.
Simple VBScript (this is just a portion of it):
Dim Wshso : Set Wshso = WScript.CreateObject("WScript.Shell")
Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")
If WScript.Arguments.Count = 1 Then
If FSO.FileExists(Wscript.Arguments.Item(0)) = true and FSO.FolderExists(Wscript.Arguments.Item(0)) = false Then
Alert "You dragged a file, not a folder! My god." & vbcrlf & "Script will terminate immediately", 0, "Alert: User is stupid", 48
WScript.Quit
Else
targetDir = WScript.Arguments.Item(0)
Wshso.Popup targetDir
End If
Else
targetDir = Wshso.SpecialFolders("Desktop")
Alert "Note: No folder to traverse detected, default set to:" & vbcrlf & Wshso.SpecialFolders("Desktop"), 0, "Alert", 48
End If
If it is a normal path without Unicode characters, it's fine. But in this case:
Directory: 4minute (포미닛) - Hit Your Heart
Then it will show something like 4minute (?) - Hit Your Heart
And if I do a FolderExists it can't find the dragged folder.
Is there any workaround to support Unicode named Folders?
Thanks!
I'll edit if this is not clear enough
This does seem to be a problem peculiar to the Windows Script Host's DropHandler shell extension. Whereas:
test.vbs "C:\포미닛.txt"
C:\WINDOWS\System32\WScript.exe "test.vbs" "C:\포미닛.txt"
both work when typed from the console (even if the console can't render the Hangul so it looks like ?), a drag and drop operation that should result in the same command goes through a Unicode->ANSI->Unicode translation that loses all characters that aren't in the current ANSI code page. (So 포미닛 will work on a default Korean Windows install but not Western.)
I'm not aware of a proper way to fix the problem. You could perhaps work around it by changing the DropHandler for .vbs files in the registry:
HKEY_CLASSES_ROOT\VBSFile\ShellEx\DropHandler\(Default)
from the WSH DropHandler ({60254CA5-953B-11CF-8C96-00AA00B8708C}) to {86C86720-42A0-1069-A2E8-08002B30309D}, the one used for .exe, .bat and similar, which doesn't suffer from this issue. You would also probably have to change the file association for .vbs to put quotes around the filename argument too, since the EXE DropHandler doesn't, to avoid problems with spaces in filenames.
Since this affects argument-passing for all VBS files it would be a perilous fix to deploy on any machine but your own. If you needed to do that, maybe you could try creating a new file extension with the appropriate DropTarget rather than changing VBSFile itself? Or maybe forgo drop-onto-script behaviour and provide a file Open dialog or manual drop field instead.
For anyone landing here from Google...
Bobince's tip lead me to work around this problem by wrapping my vbscript file (myscript.vbs) in a dos batch file (mybatch.bat).
The tip was:
"Seem to be a problem peculiar to the Windows Script Host's
DropHandler shell extension whereas.... the one used for .exe, .bat and
similar... doesn't suffer from this issue."
mybatch.bat contains:
:Loop
IF "%1"=="" GOTO Continue
set allfiles=%allfiles% "%1"
SHIFT
GOTO Loop
:Continue
"myscript.vbs" %allfiles%
You may also find this code from my myscript.vbs to be helpful
For Each strFullFileName In Wscript.Arguments
' do stuff
Next
Based on DG's answer, if you just want to accept one file as drop target then you can write a batch file (if you have it named as "x.bat" place VBScript with filename "x.bat.vbs" at same folder) that just contains:
#"%0.vbs" %1
the # means to not output the row on the display (I found it to show garbage text even if you use chcp 1250 as first command)
don't use double-quotes around %1, it won't work if your VBScript uses logic like the following (code I was using below was from http://jeffkinzer.blogspot.com/2012/06/vbscript-to-convert-excel-to-csv.html). Tested it and it works fine with spaces in the file and folder names:
Dim strExcelFileName
strExcelFileName = WScript.Arguments.Item(0) 'file name to parse
' get path where script is running
strScript = WScript.ScriptFullName
Dim fso
Set fso = CreateObject ("Scripting.FileSystemObject")
strScriptPath = fso.GetAbsolutePathName(strScript & "\..")
Set fso = Nothing
' If the Input file is NOT qualified with a path, default the current path
LPosition = InStrRev(strExcelFileName, "\")
if LPosition = 0 Then 'no folder path
strExcelFileName = strScriptPath & "\" & strExcelFileName
strScriptPath = strScriptPath & "\"
else 'there is a folder path, use it for the output folder path also
strScriptPath = Mid(strExcelFileName, 1, LPosition)
End If
' msgbox LPosition & " - " & strExcelFileName & " - " & strScriptPath
Modify WSH DropHandler ({60254CA5-953B-11CF-8C96-00AA00B8708C}) to {86C86720-42A0-1069-A2E8-08002B30309D} and add this function to convert short path to long:
Function Short2Long(shortFullPath)
dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(shortFullPath)
Set app = CreateObject("Shell.Application")
Short2Long = app.NameSpace(f.ParentFolder.Path).ParseName(f.Name).Path
end function