AppleScript choose file dialog box with default location not working - applescript

So there goes four hours of my life that I will never get back.
I'm trying to do something seemingly simple...
I want to open a file select dialog box and specify the default location.
I actually got this to work using the following...
choose file with prompt "Please choose a file:" of type {"XLSX", "APPL"}
default location "/Users/lowken/Dropbox/"
This works and does exactly what I want (the file dialog opens up in the Dropbox folder).
However when I try to use a string variable it doesn't work...
set strPath to "/Users/lowken/Dropbox/"
choose file with prompt "Please choose a file:" of type {"XLSX", "APPL"}
default location strPath
Now the dialog box opens in the root directory of the hard drive :-(
It seems that the default location is being ignored however if the path is not correct Applescript does raise a error.
I've tried casting the value as a string. I even tried to use the POSIX format...
"Macintosh HD:Users:lowken:Dropbox"
This didn't format didn't work at all.
I'm running OS X Yosemite 10.10.4 on a mid 2012 MacBook Pro.
Can anyone help me?

default location of choose file expects an alias specifier rather than a POSIX path. This is a user and startup disk name independent solution
set dropboxFolder to (path to home folder as text) & "Dropbox"
choose file with prompt "Please choose a file:" of type {"XLSX", "APPL"} default location alias dropboxFolder

Related

Applescript to ask for input, verify folder exists, and then open folder

I want to write a script using Automator that opens a folder in a particular location, after receiving user input. Doesn't have to be Applescript.
So the steps would be:
Dialog asking for Folder name
Verifying the folder exists
If exists, open folder in new finder window
if not exist, display message
Any help would be greatly appreciated.
instead of asking user to type folder name into a dialog box, why not use the standard "choose folder" which provide usual file/folder selection graphic interface ? on top of that, it will also make sure the folder selected exists !
Also it is possible to user instruction "if My_Folder exists then ..." to check if folder (or file) exists
Example of direct user selection : 5 first lines are asking folder selection in folder Documents and detect user cancellation. Next lines are just example to display result
try
set SelectFolder to choose folder with prompt "choose folder" default location "/Users/My_User/Documents"
on error number -128
set SelectFolder to ""
end try
if SelectFolder is "" then
display alert "user did not select a folder"
else
display alert "User selection is " & SelectFolder
end if
The following script does exactly what you're asking for.
on run
set thisFolder to (choose folder with prompt "Choose a folder...")
if folderExists(thisFolder) then
-- display dialog "The folder exists." buttons {"OK"} default button 1
tell application "Finder" to open thisFolder
else
display dialog "The folder has been deleted" buttons {"OK"} default button 1
end if
end run
on folderExists(theFolder)
tell application "Finder"
try
set thisFolder to the POSIX path of theFolder
set thisFolder to (POSIX file thisFolder) as text
--set thisFolder to theFolder as alias
return true
on error err
return false
end try
end tell
end folderExists
That said, note that a folder that has been selected using AppleScript's Choose Folder command must always exist. It can't be selected if it doesn't exist. Therefore, my first thought is that you don't need this, but if you need to check whether a folder exist for a different reason, then the folderExists function will do the job.
You could use the following script to do what you're asking for (no choose folder, this script uses a dialog like you asked for):
set folderChosen to text returned of (display dialog "Enter the path to the folder you want to open:" default answer "")
try
do shell script "ls " & folderChosen
do shell script "open " & folderChosen
on error
display alert "Folder Doesn't Exist" message "The folder path you entered doesn't exist. Make sure to enter a path, e.x. /Users/USERNAME/MyFolder." as critical
end try
This AppleScript uses do shell script "ls " & folderChosen to verify if the folder exists, and then opens the folder if ls successfully runs, indicating the folder path exists. Otherwise, it displays an alert with a warning sign explaining the path they entered doesn't exist.
If you want this to run in automator, you could use Automator's Run AppleScript feature.
I hope this solution helps you solve your problem!

Copy a file from a network folder using applescript

Iam very new to applescript and am currently trying to copy a file from a network share. I have an iMac server on my network and this script will be distributed to all the mac computers connected to it.
Below is the code i have at the moment :-
set mycopypath to "Volumes:Work Basket:LabRat:LabRat For Client Side.xlsx" as alias
--set mycopypath to choose file
tell application "Finder" to set startup_Disk to (name of startup disk)
set mypastepath to startup_Disk & ":Users:Arjun:Desktop:" as alias
tell application "Finder" to duplicate mycopypath to mypastepath with replacing
In the above code , 'Work Basket' in the 'mycopypath' is the shared network folder. Currently when i run this code i get an error stating - File Volumes:Work Basket:LabRat:LabRat For Client Side.xlsx wasn’t found.
But when i use the commented out part set mycopypath to choose file it works fine. Also, if i use the above code to copy paste a file from one location to another on the local computer itself, it works fine. The network folder "Work Basket" is mounted in both cases.
I'v been at it for quite a few hours and must have tried many combinations of file paths but haven't been able to find a solution.Any help will be great.
Try the following solution:
When you only uncomment the set mycopypath to choose file and comment the rest, you will get the correct path as a result in the bottom half of Script Editor. (See red arrow in screenshot).
After that copy that path to the set mycopypath to "" part and restore the original comments. This should work.
EDIT
I believe this is how to mount a network disk:
tell application "Finder" to mount volume "Your disk path/name"
EDIT 2
Mounting a network disk with user and pass:
mount volume "afp://192.168.200.1/" as user name "your username" with password "your password"
Unlike POSIX paths where a path starts always with a / representing the startup volume and a path to an external volume starts with /Volumes, HFS paths (colon separated) start always with a disk name.
This is a shorter version of your code, the Finder has a property desktop
set mycopypath to "Work Basket:LabRat:LabRat For Client Side.xlsx" as alias
tell application "Finder" to duplicate mycopypath to desktop with replacing

TextExpander and AppleScript to get file path on Mac

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

AppleScripting an app to change background of Notification Center in OSX 10.8

here's my code:
set NCBGPath to path ("Machintosh hd:System:LIbrary:Core Services:Notification Center:Contents:Resources")
set NCBackground to {"linen.tiff"}
set themeFolder to choose folder with prompt "Choose a Theme"
tell application "Finder"
if exists file (themeFolder & NCBackground) then
copy file (themeFolder & NCBackground) to NCGBPath
end if
end tell `
What do I need to change to make it work? It should let you choose a folder, if in that folder there's a file called linen.tiff then copy that file to a set path:
/System/Library/CoreServices/Notification Center/Contents/Resources
replacing the one that already exist...
In troubles setting the path and making it work
You seem to have all of your paths messed up. You're just not using them correctly. Also the Finder does not have a "copy" command. It has a "duplicate" command though. However because you're performing the duplicate to a restricted location I would use the cp shell command instead and run it with "administrator privileges".
So the below code will do what you are trying to do (I did not test it). However I doubt it's a good idea and don't know if it will even work. Normally just replacing a file won't make a change like you're expecting without restarting notification center. Also, as I mention in the code, you're going to have a file permissions issue. Files in that folder have special permissions that your copied file will not have. Finally, it's not a good idea to touch things in the /System directory.
With all that being said, if you still want to continue then give this a try.
set NCBGPath to "/System/Library/CoreServices/Notification Center/Contents/Resources/"
set NCBackground to "linen.tiff"
set themeFolder to (choose folder with prompt "Choose a Theme") as text
set themePath to themeFolder & NCBackground
set posixNCPath to NCBGPath & NCBackground
set shouldCopy to false
tell application "Finder"
if exists file themePath then set shouldCopy to true
end tell
if shouldCopy then
do shell script "cp " & quoted form of POSIX path of themePath & space & quoted form of posixNCPath with administrator privileges
-- you probably should correct the file permissions too as the copied file probably won't have the proper owner and stuff
else
display dialog "Could not find the background file in the chosen folder."
end if

AppleScript Droplet on DMG not working

Hey I have the following AppleScript saved as a Droplet.
It is saved on a DMG file like this one http://dl.dropbox.com/u/1839051/TestDMG.dmg
The Problem is, while some can drag the template onto the Droplet and have it working, when I try to drag the template onto the droplet the crossed-out circle-symbol shows up indicating that this action is not possible. Nothing happens, the file is not copied.
Does anyone have any idea why I have this problem and how it can be fixed?
Thanks in advance guy.
on open thefiles
set outputFolder to (path to application support folder from user domain as text) & "iWork:Pages:Templates:My Templates:"
do shell script "/bin/mkdir -p " & quoted form of POSIX path of outputFolder
tell application "Finder"
duplicate thefiles to outputFolder
end tell
end open
Rather than using a droplet and having the user to drag the files onto the droplet, why not just make an installer so the user only has to double-click the installer? It would be easier and also probably avoid your problem. I also added some error handling in your code because it's just prudent to do that with shipping code. We also tell the user what happened.
NOTE: you also had an error in your code. The outputFolder is a string. The Finder requires a file specifier. To make the string into a specifier you add either the word "file" or "folder" in front of the string path. Your code may have worked but the proper way to write it is with a specifier. Other applications may not take the string path but they will all take the specifier... so get in the habit of using them instead of strings.
try
-- create the output folder if necessary
set outputFolder to (path to application support folder from user domain as text) & "iWork:Pages:Templates:My Templates:"
do shell script "/bin/mkdir -p " & quoted form of POSIX path of outputFolder
-- find the templates on the dmg disk
set myPath to path to me
tell application "Finder"
set myContainer to container of myPath
set templateFiles to (files of myContainer whose name extension is "template") as alias list
end tell
-- copy the templates to the output folder
-- NOTE: the script will error if any of the templates already exist
-- therefore we use a repeat loop and duplicate each file separately with a try block
-- around it to avoid errors in case some templates have already been installed.
tell application "Finder"
repeat with aTemplate in templateFiles
try
duplicate aTemplate to folder outputFolder
end try
end repeat
end tell
-- tell the user everything was OK
tell me to activate
display dialog "The templates were successfully installed! You may now use them in Pages." buttons {"OK"} default button 1 with title "Templates Installer" with icon note
on error
tell me to activate
display dialog "There was an error installing the templates. Please manually install them by copying them to the following folder." & return & return & (POSIX path of outputFolder) buttons {"OK"} default button 1 with title "Templates Installer"
end try
This looks to be a permissions issue, and I have to wonder if the differential between those who can and those who can't have something to do with which OS they are running. I'm running Mac OS 10.6 as an administrator and I was unable to perform the action in the DMG. But I was able to perform the action if I dragged both files out of the DMG and onto my Desktop.
If you need to install files in specific locations to the hard drive to support your project, then I would recommend making an installer (and a matching uninstaller as well) as opposed to the setup you have presented.

Resources