I try to create "Save as..." dialog in Mac OS X. But I don't want to use QFileDialog::getSaveFileName() function, because dialog that created by this function is NOT truly-native in Mac OS X Lion. So I decide to create dialog as QFileDialog object:
auto export_dialog( new QFileDialog( main_window ) );
export_dialog->setWindowModality( Qt::WindowModal );
export_dialog->setFileMode( QFileDialog::AnyFile );
export_dialog->setAcceptMode( QFileDialog::AcceptSave );
All works fine, except one problem. I cannot set default name for saved file, so user must type this name manually every time. I know that function QFileDialog::getSaveFileName() allows to set default filename via third argument, dir (http://qt-project.org/doc/qt-4.8/qfiledialog.html#getSaveFileName). But how to set this default name without this function?
I can set default suffix for saved file via QFileDialog::setDefaultSuffix() function, but I need to set whole default name, not only default suffix.
I've tried to use QFileDialog::setDirectory() function, but it sets only directory where to save, without name of saved file.
I use Qt 4.8.1 on Mac OS X Lion.
I have found that using selectFile("myFileName"); only works if the file actually exists. In my case, the intent is to create a new file with the option of overwriting an existing file.
The solution that worked for me (Qt 5.3.2) was as follows:
QFileDialog svDlg;
QString saveFileName = svDlg.getSaveFileName(this, caption, preferredName, filter);
In the above example, preferredName is a QString that contains "C:/pre-selected-name.txt"
Restating what was in the comments for future visitors, the following line puts "myFileName" as the default name in the QFileDialog:
export_dialog->selectFile("myFileName");
Discussion: http://www.qtcentre.org/threads/49434-QFileDialog-set-default-name?highlight=QFileDialog
Not-so-helpful docs: http://qt-project.org/doc/qt-4.8/qfiledialog.html#selectFile
QString dir = QDir::homePath();
QString name = "test.txt";
QFileDialog::getSaveFileName(nullptr, tr("save file"), dir + "/" + name, tr("TXT (*.txt)"));
If you set "dir" argument, and dir is "file"(exist or not), in windows you will have default name.
With the current QT-version (5.x) you can set your preferred file-name with the argument directory in the QFileDialog.getSaveFileName() function call:
QFileDialog.getSaveFileName( directory = 'preferredFileName.txt' )
docs: http://doc.qt.io/qt-5/qfiledialog.html#getSaveFileName
Related
I have a Xamarin application running on Windows, and I have a method which includes an opening of a pdf file like this:
var psi = new ProcessStartInfo
{
FileName = "cmd",
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
CreateNoWindow = true,
Arguments = $"/c start {filename}"
};
Process.Start(psi);
When this executes, the windows opens a dialog with the following message:
Windows cannot access the specified device, path, or file. You may not have the appropriate permissions to access the item.
The filename is a pdf file located in the LocalApplicationData, and I also have a database there, and the application is normally creating a database there and manipulates with it, so it should have a permission to access that folder. Also, when I run that pdf with double-click outside the application, the pdf opens normally with Chrome. How to solve this?
Unless you have a file there called "cmd" this won't work, as you have declared your filename as a string with the value "cmd".
How to (easily) get current file path in Sublime Text 3
I don't often use ST console (I used it only once to install package manager), but I suppose it could be good way to :
get current file path like some kind pwd command.
But it doesn't work.
Does anyone know an easy way to get current file path?
to clipboard : better not a strict objective in the answer
not necessary by ST command, maybe package?
Right click somewhere in the file (not on the title tab) --> Copy file path
If you don't want to use the mouse, you could set up a keyboard shortcut as explained here https://superuser.com/questions/636057/how-to-set-shortcut-for-copy-file-path-in-sublime-text-3
To easily copy the current file path, add the following to Key Bindings - User:
{ "keys": ["ctrl+alt+c"], "command": "copy_path" },
Source
Key Bindings - User can be opened via the command palette (command + p on OSX)
Easy to understand using image. On Right Click you will get this.
Transcribed code in image for convenience:
import sublime, sublime_plugin, os
class CopyFilenameCommand(sublime_plugin.TextCommand):
def run(self, edit):
if len(self.view.file_name()) > 0:
filename = os.path.split(self.view.file_name())[1]
sublime.set_clipboard(filename)
sublime.status_message("Copied file name: %s" % filename)
def is_enabled(self):
return self.view.file_name()... # can't see
Mac OS X - Sublime Text 3
Right click > Copy File Path
A lot of these answers involve touching the mouse. Here's how to do get the path without any mouse clicks using SideBarEnhancements
Install SideBarEnhancements using PackageControl.
Click super + shift + P to open the command palette
In the command palette begin typing path until you see File: Copy Path
Select File: Copy Path
Now the path to file you are working in is copied into your clipboard.
There is a Sublime Package which gives your current file location inside a status bar. I just cloned them directly to my /sublime-text-3/Packages folder.
git clone git#github.com:shagabutdinov/sublime-shell-status.git ShellStatus;
git clone git#github.com:shagabutdinov/sublime-status-message.git StatusMessage;
You have to check/read the description on GitHub. Even it is listed in package control it would not install properly for me. You can actually edit the shell output as you want. If you have the right skills with python/shell.
Looks like this (Material Theme)
If you're like me and always click on items in the sidebar just to realize that copying the path only works when clicking in the editor area, have a look at the SideBarEnhancements package. It has a huge bunch of options to copy file paths in a variety of different ways.
Installation is available via Package Control (despite the webpage only mentions installation via manual download).
Note: The package “sends basic, anonymous statistics”. The webpage explains how to opt out from that.
Go to this link. The code in the link is given by robertcollier4.
Create a file named CpoyFileName.py or whatever you like with .py extension.
Save the file in Sublime Text 3\Packages\User folder. Then paste the above given key bindings in your Preferences: Key Bindings file.
Now, you can use the specified key bindings to copy just filename or total (absolute) filepath.
Please note that the filename or filepath do contain file extension.
Fastest Solution ( No Packages Needed + Comprehensive ):
Folder Path:
Folder in "Sidebar"
Right Click
"Find In Folder"
"Where" field contains everything you need
File Path:
File in current "Tab"
Right Click
"Copy File Path"
I'm working on a simple AppleScript to make a copy of an iMovie project file. I have added this property to the script:
property iMovieProjects : alias (home directory of (system info) as string) & "Movies:iMovie Projects"
This gives me the error
File alias Macintosh HD:Users:my user name:Movies:iMovie Projects of wasn't found
What's the correct path? I tried iMovie Project.localized but that doesn't work either.
Try this, looks like you need to use the Finder. Also i added a little simplification on getting the home path.
property iMovieProjects : ""
tell application "Finder" to set iMovieProjects to alias ((home as string) & "Movies:iMovie Projects")
EDIT: Heres a one line solution that does not use the Finder, based on ideas from mklement0 & regulus6633
property iMovieProjects : alias ((path to movies folder as text) & "iMovie Projects.localized")
It appears this folder is some kind of special bundle type folder and thats why we were needing the finder to parse it correctly. Using its full .localized name resolves it.
UPDATE: As adamh discovered, the reason for the alias error is because the real name of the folder is "iMovie Projects.localized". Get info on the folder and you will see it.
I would add that an easier method to get to a folder is using the "path to" command. Using that you can get to virtually every known folder. In your case we can get directly to the Movies folder. You can look in the applescript dictionary of the standard additions to see all of the folders it knows. As such I would reference that folder as follows.
set iMovieProjects to alias ((path to movies folder as text) & "iMovie Projects.localized:")
Finally you'll notice that I did not use a property for iMovieProjects. That's because when you compile a script a property will hard-code the path into the script... meaning that the script will only work for this particular user. If the script is used by another user it will still point to the Movies folder of the person at compile time. Thus we don't use a property and the script will work for any user.
Good luck.
Update2 (thanks, #adamh): Even though the iMovie projects folder name appears as "iMovie Projects" in Finder in English-language locales, the actual - locale-independent - name is "iMovie Projects.localized". (If you want to refer to the folder by its localized name, prefix the set command with tell application "Finder", as in #adamh's answer's first snippet.)
You simply need to change how you parenthesize - the string to pass to alias must be built in its entirety inside parentheses:
property iMovieProjects : ""
set iMovieProjects to alias ( ¬
(home directory of (system info) as string) & "Movies:iMovie Projects.localized" ¬
)
Update1: #regulus6633 makes the excellent point that you shouldn't initialize a property with a specific user's path, as it will get compiled into the script. Thus, the above snippet initializes the property to an empty string and then assigns a value dynamically - which will then reflect the current user's path - in a separate set ... to statement.
Your original statement inadvertently creates a list with 2 elements, whose first element is a an alias of your home directory and whose second element is the string "Movies:iMovie Projects".
Simplified version with the path to command demonstrated in #regulus6633's answer (the same need to parenthesize applies):
property iMovieProjects : ""
set iMovieProjects to alias ( ¬
(path to movies folder as text) & "iMovie Projects.localized" ¬
)
I am new to octave and need to plot a 2-D graph with customized title either as a file name or current directory name. I tried passing pwd in the plot.m file but it gives me a complete path instead of directory name only. Actually all I need is a customized title without manually hard coding the string inside xlabel('strings').
I don't have Octave but this works on MATLAB:
current_directory_name = pwd;
current_directory_split = regexp(current_directory_name,'/','split');
string_of_interest = current_directory_split(end);
xlabel(string_of_interest);
I am assuming you are on *NIX computer. For Windows computer, change / to \ in the split command.
If I understood your question correctly, you want a portable way of retrieving the file name w/o the directory name. Use fileparts():
[dir, name, ext, ver] = fileparts(pwd)
If you later decide to join strings, use filesep which is portable no matter if you're on Unix or not.
I am trying get the parent folder of a Windows user's profile path. But I couldn't find any "parameter" to get this using SHGetSpecialFolderPath, so far I am using CSIDL_PROFILE.
Expected Path:
Win7 - "C:\Users"
Windows XP - "C:\Documents and Settings"
For most purposes other than displaying the path to a user, it should work to append "\\.." (or "..\\" if it ends with a backslash) to the path in question.
With the shell libary version 6.0 you have the CSIDL_PROFILES (not to be confused with CSIDL_PROFILE) which gives you what you want. This value was removed (see here), you have to use your own workaround.
On any prior version you'll have to implement your own workaround, such as looking for the possible path separator(s), i.e. \ and / on Windows, and terminate the string at the last one. A simple version of this could use strrchr (or wcsrchr) to locate the backslash and then, assuming the string is writable, terminate the string at that location.
Example:
char* path;
// Retrieve the path at this point, e.g. "C:\\Users\\username"
char* lastSlash = strrchr(path, '\\');
if(!lastSlash)
lastSlash = strrchr(path, '/');
if(lastSlash)
*lastSlash = 0;
Or of course GetProfilesDirectory (that eluded me) which you pointed out in a comment to this answer.