I use this to get a prompt where i can select a file
set thePath to quoted form of POSIX path of (choose folder with prompt "whatever)
How can i see hidden files in the "choose folder with prompt"?
Allowing to see hidden files in finder doesn't do anything different.
You simply need to add with invisibles:
set thePath to quoted form of POSIX path of ¬
(choose folder with prompt "whatever" with invisibles)
You can find the full syntax of the choose folder command in the Standard Additions dictionary: in AppleScript Editor, press Cmd-Shift-O (or select File > Open Dictionary...) and select StandardAdditions.osax from the list.
Related
I'm trying to make script or automate unrar to unrar a selected file to a specific folder (hard coded).
I want the following code to be run in terminal by clicking a button in finder or a keyboard shortcut while I have a file selected.
unrar e <path_to_selected_file.rar> <hard_coded_path>
How can I do this in the best way?
If your destination path is hardcoded, then I suggest you to use Automator.
First create a Service. Select on top, "get the file" in application "Finder".
Then add only one action : "run an Applescript".
In that action, the default script starts with variable "input". This variable will contains the list of all selected files while you're doing a right click on them in the Finder. Build your script to loop through files of that list, using POXIS function to convert the finder path (myUser:myfolder:myfile) to shell path (myUser/myfolder/myfile). With this path, use the "do shell script" command to run your "unbar" script.
When saved and tested, you can also define a shortcut key for that Service (in System Preferences).
Here is the script which should be in your Applescript Action :
on run {input, parameters}
set Destination to path to desktop folder -- User Desktop by default. can be changed
set PosixDest to POSIX path of Destination
set SelectedFiles to input
repeat with myFile in SelectedFiles -- loop through each selected file
set PosixF to POSIX path of myFile -- convert Finder path to Unix path
try -- try block to handle error during unbar
do shell script "unrar e " & (quoted form of PosixF) & " " & (quoted form of PosixDest)
end try
end repeat -- next file
return input
end run
This example is running as long as you select compressed file (to accept the unbar command). To be more safe, you should just add a test to your file, to check if it is a file OK for unbar. If not, just do nothing.
How would you only allow the user to select folders when using the 'choose from file' prompt? Also, once they select the file, how would you get the text returned for the path to the file. (So the text returned would be /Users/myname/Desktop/afile)
My current unworking code is as follows:
set d to the text returned of (choose file with prompt "Please choose a file:" of type {"??", "??"} default location "/Users/myname/Desktop")
When running this in applescript, it only lets you choose files, and once you do so, I get the error error "Can’t get text returned of alias \"Macintosh HD:Users:myname:Desktop:filename.extention:\"." number -1728 from text returned of alias "Macintosh HD:Users:myname:Desktop:filename.extention:"
choose file returns an alias specifier to the chosen file. You mixed it up with display dialog which returns a record containing text returned.
To get the slash separated POSIX path put POSIX path in front of the expression
set d to POSIX path of (choose file with prompt "Please choose a file:" default location (path to desktop))
If you want to select folders use choose folder, same syntax
set d to POSIX path of (choose folder with prompt "Please choose a folder:" default location (path to desktop))
I would like to create a TextExpander snippet that will prompt me to choose a file in the Finder and copy the file path to the text editor I am working in.
It should work like this:
I am working in NVAlt
I enter the TextExpander command (e.g. ;path)
I am prompt top choose a file
The file path is copied to the note I am working in.
I figured I'll do it using AppleScript, so I tried this:
set source_folder to choose file with prompt "Please select a file."
tell application "System Events"
set item_list to POSIX path of every disk item of source_folder
end tell
return "path:" & item_list
But it is not returning anything to the text note I am working in...
Any suggestion how I should do it?
PS: I used the POSIX command to the folder name as ".../.../.../" instead of "...:...:..."
There are multiple things wrong in the script but instead of trying to correct your script I've looked what textExpander tells me to do. On the help section about textExpander on smilesoftware.com site they say:
The script executes in the context of the TextExpander application.
The script can perform various actions, but the snippet will expand to
whatever text is returned.
When looking at the your goal you simply need to coerce the returned alias from the choose file command into a string and return that. So a single line like below should be enough to use to return an HFS path to a file
return choose file with prompt "Please select a file." as string
if you want to return the file path as POSIX Path you only need this:
return POSIX path of (choose file with prompt "Please select a file.")
If you want the path to be prefixed with "path:" like in your example code:
return "path:" & (choose file with prompt "Please select a file.")
NOTE: there is an implicit coerce of the choose file results, no explicit coercion is needed.
EDIT: I've downloaded textExpander myself and it seems that showing dialog in the context of textExpander will behave wrong. So what I did was looking up the front most application and showing the dialog there, it's also better looking. Then I tell that application explicitly to show the choose file prompt and return the result of the choose file prompt. Here is the code:
tell application "System Events"
set applicationName to (name of every process whose frontmost is true) as string
end tell
using terms from application "AppleScript Editor"
tell application applicationName
set expansionString to (choose file "Please select a file.") as string
end tell
end using terms from
return expansionString
can I use AppleScript to choose either file or folder in one time?
Now I could use
tell application "SystemUIServer" to return POSIX path of (choose file)
or
tell application "SystemUIServer" to return POSIX path of (choose folder)
to get file or folder. However I cannot get file or folder in one time.
No, you can't do it with "choose file" or "choose folder" verbs, but choosing a file or folder (or multiple files/folders) is supported by the underlying NSOpenPanel. So you can do it with AppleScriptObjC. Here's an example using ASObjCRunner (derived from here):
script chooseFilesOrFolders
tell current application's NSOpenPanel's openPanel()
setTitle_("Choose Files or Folders") -- window title, default is "Open"
setPrompt_("Choose") -- button name, default is "Open"
setCanChooseFiles_(true)
setCanChooseDirectories_(true)
setAllowsMultipleSelection_(true) -- remove if you only want a single file/folder
get its runModal() as integer -- show the panel
if result is current application's NSFileHandlingPanelCancelButton then error number -128 -- cancelled
return URLs() as list
end tell
end script
tell application "ASObjC Runner"
activate
run the script {chooseFilesOrFolders} with response
end tell
ASObjCRunner converts a NSArray of NSURL objects into an AppleScript list of files; the results can look something like:
{file "Macintosh HD:Users:nicholas:Desktop:fontconfig:", file "Macintosh HD:Users:nicholas:Desktop:form.pdf"}
Firstly, you don't need a tell for that.
POSIX path of (choose file)
Secondly, it is not clear why you need this. Do you mean you want to select a file and it's folder? That's not how you do it; you select the file then parse the file path for the containing folder or use one of the many methods to do that, like
set f to (choose file)
set posixF to POSIX path of f
tell application "Finder" to set filesDir to container of f as alias as text
set posixDir to POSIX path of filesDir
{f, posixF, filesDir, posixDir}
If you want to be able to select multiple folders and files at the same time, I don't think there is a "pure applescript" way to do this (aside from using a drag-drop aware script application).
Sorry I don't have more of a foundation but I know nothing of AppleScript. Basically I want to right-click on a file or folder and run a script that will copy it to a new location with a constant directory structure. So obviously the scripting is way off, but something like this..
path = /Volumes/RENDERS/ThisShow/ThisShot/ThisShow_ThisShot_v10/
newPath = from 'path' ReplaceText("/Volumes/RENDERS/" , "/Volumes/Raid-Renders/")
tell application "Finder" to duplicate file 'path' to 'newPath' with replacing
In this example "path" would be the file or folder that was right clicked on when launching the script. The new folder in this example would be "/Volumes/Raid-Renders/ThisShow/ThisShot/ThisShow_ThisShot_v10/". So the idea is that it would copy "ThisShow_ThisShot_v10" folder and its content to "/Volumes/Raid-Renders/ThisShow/ThisShot/"
Thanks for any help. I know there isn't a lot to go on here.
This should do it:
set thisFolder to the POSIX path of (choose file)
set the destinationFolder to (replaceText(thisFolder) as POSIX file as alias)
tell application "Finder" to duplicate (thisFolder as POSIX file as alias) to the destinationFolder with replacing
on replaceText(this_folder)
set AppleScript's text item delimiters to "RENDERS"
set these_items to every text item of this_folder
set AppleScript's text item delimiters to "Raid-Renders"
return these_items as string
end replaceText
As far as I know, you can't assign an AppleScript to a right-click menu. However, you can create a menu bar script. To do this, first save this script as a regular script file in the Scripts folder of the local Library folder.
If you have a little icon in your main menubar (located at the top of the screen) that looks like a scroll (formerly known as the Script Menu), the script should appear somewhere in that menu. If you don't see the icon, run AppleScript Utility (located at /Applications/AppleScript/AppleScript Utility) and check the Show Script Menu in menu bar checkbox.
Now, all you have to do to run the script is open up the Script Menu, find your script, and just click on it once. Questions? Ask. :)