Shell.Application BrowseForFolder - how do I get full path of Folder object? - wsh

I have the following script, that I launch using wscript:
Set sh = CreateObject("Shell.Application")
Set rv = sh.BrowseForFolder(0, "Now browse...", 1)
WScript.Echo rv
How can I obtain the full path of the selected folder?
The documentation for the Folder object that is returned by BrowseForFolder gives nothing appropriate.
Or maybe I should use something completely different for browsing for folders in wscript...

rv.Self.Path, discussed here in detail: How Can I Show Users a Dialog Box That Only Lets Them Select Folders? at Hey, Scripting Guy! Blog.
Set sh = CreateObject("Shell.Application")
Set rv = sh.BrowseForFolder(0, "Now browse...", 1)
If rv Is Nothing Then
WScript.Echo "Nothing chosen"
Else
WScript.Echo rv.Self.Path
End If

Related

Applescript: "File Already Open" when writing to new file

I'm creating a text file whose file name will consist of constant and variable strings. For whatever reason, I'm getting an error saying "[file name] is already open" when I'm actually just creating it. The file is created, but none of my content makes it into the file.
All of the fixes I've tried end in another error saying "network file permission."
Also, I should mention that my new file is going into the same container as another file that is used to create the new file, which is where the filePathAlias comes in.
Any ideas? Thanks in advance!
Here's the script:
-- get the file --
set filePathAlias to (choose file with prompt "** Choose File **")
set filePath to filePathAlias as string
tell application "Finder"
set fileName to name of filePathAlias
set containerPath to (container of filePathAlias) as string
end tell
set filePath to filePathAlias as string
-- get file container --
tell application "Finder"
set containerPath to (container of filePathAlias) as string
end tell
-- combine file name variable with constant suffix --
set finalFile to locationName & "_RMP_2014.txt"
-- create the file (THIS IS WHERE THE ERROR COMES IN) --
set myFile to open for access (containerPath) & finalFile with write permission
set listTextFinal to "text goes here"
try
write listTextFinal to myFile as text
close access myFile
on error
close access myFile
end try
You didn't give an example path for filePathAlias or locationName. I was unable to reproduce the file already open error. I can reproduce the network file permission error...So:
set filepathalias to ((path to desktop folder as string) & "test" as string) as alias
--alias of folder on desktop called test... massaged well to be an alias that can later be converted to string when making containerPath
set locationName to "stuff you left out" --Just a name I assume...
-- get file container --
tell application "Finder"
set containerPath to ((container of filepathalias) as string)
end tell
-- combine file name variable with constant suffix --
set finalFile to locationName & "_RMP_2014.txt"
-- create the file (THIS IS WHERE THE ERROR COMES IN) --
set myFile to open for access (containerPath) & finalFile with write permission
set listTextFinal to "text goes here"
try
write listTextFinal to myFile as text
close access myFile
on error
close access myFile
end try
This works perfectly, if you were to be working to the desktop. The problem appears to be in the stage of getting the path correct.
Without all the massaging to the filepathalias I did in the first line we get the network file error. The file is trying to save in places you can not save to....
You will need to verify the filepathalias, containerPath, & finalFile all contain the information you'd expect.
Right below where the finalFile is set try this from the editor:
return {filepathalias as string, containerPath as string, finalFile as string}
my result from the above:
{"mac:Users:lithodora:Desktop:test:", "mac:Users:lithodora:Desktop:", "stuff you left out_RMP_2014.txt"}
That is similar to what you should expect.
-- Convert the file to a string
set desktop_path to POSIX path of (path to desktop)
set theFile to desktop_path & "subject_lines.csv" as POSIX file
set theFile to theFile as string
-- Open the file for writing
set theOpenedFile to open for access file theFile with write permission
write "Hello Kitty" to theOpenedFile
-- Close the file
close access theOpenedFile
this would cause the error "File file Mac:Users:jun:Desktop:subject_lines.csv is already open."
If delete "set theFile to theFile as string", and makes the following changes:
set theFile to desktop_path & "subject_lines.csv" as string
then the error "Network file permission error."

Save current document as .html with same name and path

I'm working on a script for FoldingText which will convert a FoldingText outline (basically a Markdown Text file) into a Remark presentation (an HTML script which makes slideshows from Markdown files). The script works, but I'd like to make the following improvement:
Instead of asking for the name and location to save the HTML file, I'd like to grab the name of the current document and save it as an HTML file (with the same name) in the current folder. The script should fail nicely if there is already a document with that name (offering to either write-over the document or save as a new document with a different name).
The script I'm using for writing to the file was from these forums. The first part is:
on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean)
try
set the target_file to the target_file as text
set the open_target_file to ¬
open for access file target_file with write permission
if append_data is false then ¬
set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof as «class utf8»
close access the open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file
And the second part is:
set theFile to choose file name with prompt "Set file name and location:"
my write_to_file(finalText, theFile, true)
Thanks.
FoldingText should have some way of retrieveng the path of the document. I've not found any free dowonload fo the application, so I've not benn able to check by myself, but you should be able to find it if you view the dictionary of the application.
My guess is that there's a property like 'path of', or 'file of' for the FoldingText document:
You will probably end up with something like this:
set theDocPath to file of front document of application "FoldingText"
tell application "Finder"
set theContainer to container of theFile
end tell
set newPath to (theContainer & "export.html) as string
repeat while (file newPath exists)
set newPath to choose file name with prompt "Set file name and location:"
end repeat
my write_to_file(finalText, newPath, true)

Iexpress - extraction path

I am going to create a self extracting archive but I have got a problem connecting with the default path of the extraction. I would like to extract my files in the same path as the self-extraction archive program. Unfortunately, the files are extracting in another path (C:\Users\computer\AppData\Temp\IXP000.TMP). Is it possible to set the path?
I can't find any direct way to do this with IExpress, but there is a trick we can apply.
But first I'll point out that this is really easy with something like 7-Zip's 7zCon.sfx module (if all you need to do is have the archive extract to the current directory, no questions asked). So you might just want to try something other than IExpress.
Anyhow, the problem with IExpress is that, at the time our install program runs, we're no longer in the directory of the original archive; the current directory is now something like %temp%\IXP000.TMP. So we need to find the directory of our parent process – kind of a pain. Once that's known, we can just xcopy the contents of the archive over to the destination folder.
In VBScript, it would look something like this:
Option Explicit
Dim objShell, objWMI
Dim objCmd, intMyPid, intMyParentPid, objMyParent
Set objShell = CreateObject("WScript.Shell")
Set objWMI = GetObject("winmgmts:root\cimv2")
Set objCmd = objShell.Exec("cmd.exe")
intMyPid = objWMI.Get("Win32_Process.Handle='" & objCmd.ProcessID & "'").ParentProcessId
objCmd.Terminate
intMyParentPid = objWMI.Get("Win32_Process.Handle='" & intMyPid & "'").ParentProcessId
Set objMyParent = objWMI.Get("Win32_Process.Handle='" & intMyParentPid & "'")
objShell.Run "xcopy /y * " & """" & Left(objMyParent.ExecutablePath, _
InStrRev(objMyParent.ExecutablePath, ".exe", -1, vbTextCompare) -1) &_
"\""", 0, True
Your install program would then be, eg: wscript extractToOriginalLocation.vbs //B.
(Inspired somewhat by the answer to this question.)
You could always use a cmd script and echo lines of code into files in specific directories

Ruby tool that pics a folder on the local drive?

Ok so i have a ruby script that currently prompts the user for a string location of a folder on the users harddrive ...this works well
puts "\nEnter the location of the files"
loop do
print "\nLocation: "
reply = ''
reply = STDIN.gets.strip
break if File.directory?(reply)
puts File.directory?(reply)
end
But i was wondering if there was another tool other then the STDIN (which currently makes the user enter a string of the path) that will popup a folder search that the user can navigate to ...if there is anything else I can provide for anyone to help you help me..
I can ever use rails if there is no other option but i have no idea whats available to me
There are a many Ruby GUI libraries; pick one--most will have a file dialog.
For simple dialogs, Zenity is fine.
This is a file selection dialog:
file = `zenity --file-selection --title="Select a file"`.chomp
Adding one parameter changes it to a directory selection dialog:
dir = `zenity --file-selection --directory --title="Select a directory"`.chomp

How do you duplicate a file in XCode?

Anyone know a good solution?
So far I have not found a better way than using File>New file and then copying contents from old file to new.
You can probably duplicate in Finder and re-import but that's almost same amount of work: switching to finder, duplicate, import new files.
Doing this with one class is not so hard, but what to do if you need to generate 10+ similar Classes based on superclass.
In Eclipse you select file and then copy/paste it in same folder. In finder there's Duplicate.
There's a menu Edit > Duplicate. But it's ALWAYS disabled. I tried selecting various files, classes, methods. It's still disabled.
In XCode 4.2 (I know this is an old question) there is Duplicate under the File menu.
Select the file (you can select multiple files but it doesn't appear to do anything useful) in the Project Navigator and then File->Duplicate. Hooray!
In Xcode 4.5 we can duplicate using File-> Duplicate or cmd + shift + S
"Duplicate" is enabled for targets in XCode (pretty much nothing else that I know of).
If you have a substantial number of subclasses with the same starting point to replicate, why not make a class template from it? Then you can just use file->New to make new instances. It's fairly quick to do.
This is probably the simplest example:
http://www.macresearch.org/custom_xcode_templates
Otherwise, I'd simply duplicate the files in Finder as many times as you need, name them, and drag them into XCode en-masse.
Careful!
When you use duplicate ( CMD + Shift + S ) - Xcode have a problem with indexing headers.
Also when u want to make a refactoring it can be next error window:
So there a couple of ways what to do, to fix that.
Delete derived data from menu Window > Projects. Restart Xcode.
Product > Clean
You could use "Save As..."; you'd still have to go back and re-add the original files to the project, though.
It wouldn't be such a bad way to do a bunch of related classes, though: edit file, Save As "class2", edit file, Save As "class3", etc., then "Add Existing Files" and re-add all of the files but the last to your project.
I use the following perl script to duplicate a file pair in the Terminal. You give it the base name of the original and new file, and it copies the header and implementation (c/cpp/m/mm) file, then replaces all occurances of the base name with the new name, then adds them to subversion. You still have to add the new files in to Xcode and adjust the creation date in the comment (I've got a Keyboard Maestro macro for that), but its quicker than doing a lot of the steps manually. I operate with a Terminal window and four tabs pre-set to the Project, Source, Resources, and English.lproj directory which gives quick access for a lot of operations.
#!/usr/bin/perl
use lib "$ENV{HOME}/perl";
use warnings;
use strict;
our $cp = '/bin/cp';
our $svn = '/usr/bin/svn';
our $perl = '/usr/bin/perl';
our $source = shift;
our $add = 1;
if ( $source =~ m!^-! ) {
if ( $source eq '-a' || $source eq '--add' ) {
$add = 1;
$source = shift;
} elsif ( $source eq '-A' || $source eq '--noadd' ) {
$add = undef;
$source = shift;
} else {
die "Bad arg $source";
}
}
our $dest = shift;
die "Bad source $source" unless $source =~ m!^(.*/)?[A-Za-z0-9]+$!;
die "Bad dest $dest" unless $dest =~ m!^(.*/)?[A-Za-z0-9]+$!;
my $cpp;
$cpp = 'c' if ( -e "$source.c" );
$cpp = 'cpp' if ( -e "$source.cpp" );
$cpp = 'mm' if ( -e "$source.mm" );
$cpp = 'm' if ( -e "$source.m" );
die "Missing source $source" unless -e "$source.h" && -e "$source.$cpp";
die "Existing dest $dest" if -e "$dest.h" && -e "$dest.$cpp";
our $sourcename = $source; $sourcename =~ s!.*/!!;
our $destname = $dest; $destname =~ s!.*/!!;
print "cp $source.h $dest.h\n";
system( $cp, "$source.h", "$dest.h" );
print "s/$sourcename/$destname in $dest.h\n";
system( $perl, '-p', '-i', '-e', "s/$sourcename/$destname/g", "$dest.h" );
print "cp $source.$cpp $dest.$cpp\n";
system( $cp, "$source.$cpp", "$dest.$cpp" );
print "s/$sourcename/$destname in $dest.$cpp\n";
system( $perl, '-p', '-i', '-e', "s/$sourcename/$destname/g", "$dest.$cpp" );
if ( $add ) {
print "svn add $dest.$cpp $dest.h\n";
system( $svn, 'add', "$dest.$cpp", "$dest.h" );
}
In my case, one of my folder changed from one place to another place.
I have "Home" folder in Controller folder, but unfortunately it's moved from Controller folder to Manager folder.
I checked many times everything fine, but I'm getting Command PrecompileSwiftBridgingHeader failed with a nonzero exit code
But after 2 hours i realised, my folder structure changed.

Resources