fns720 Posted February 6, 2014 Share Posted February 6, 2014 I made a simple workflow to save a URL as a .webloc file - here's the relevant part: tell application "Finder" activate make new internet location file at filePath to theURL with properties {name:fileName} end tell Everything runs properly except that the "choose file name" dialog looses its focus. Unfortunately I have no idea how to set the focus to the dialog from script. Any suggestions? Link to comment
RodgerWW Posted February 6, 2014 Share Posted February 6, 2014 try something like this: tell application "Finder" activate display dialog "Give the file name:" default answer "" set theName to text returned of result set webloc to make new internet location file to theurl at desktop with properties {name:theName, location:theurl} end tell Link to comment
fns720 Posted February 6, 2014 Author Share Posted February 6, 2014 I want to see the "choose file name" dialog because I want to assign Mavericks tags to the .webloc file - so a simple "display dialog" requester is not enough for me . Link to comment
RodgerWW Posted February 6, 2014 Share Posted February 6, 2014 (edited) You may have to reference the application's save dialog ... because that is generally where they are. Putting the following into the applescript editor brings up the dialog I think you want. set resultFile to (choose file name with prompt "Save As File" default name "My File" default location path to desktop) as text if resultFile does not end with ".txt" then set resultFile to resultFile & ".txt" Edited February 6, 2014 by RodgerWW Link to comment
fns720 Posted February 6, 2014 Author Share Posted February 6, 2014 Same issue - no focus on dialog Link to comment
RodgerWW Posted February 6, 2014 Share Posted February 6, 2014 I need a bit more then ... is perhaps something still running in the script stealing focus? Link to comment
fns720 Posted February 6, 2014 Author Share Posted February 6, 2014 Thanks for your help Rodger. Here's the entire workflow script: on alfred_script(q) set defaultPath to "SYSTEM:Users:fns:Dropbox:URL" as alias tell application "Safari" to set theName to name of front document tell application "Safari" to set theURL to URL of front document set theName to replace_chars(theName, ":", " -") set text item delimiters to ":" set fileName to last text item of resultFile set filePath to text 1 thru lastpos(resultFile, ":") of resultFile set resultFile to (choose file name with prompt "Save URL" default name theURL default location filePath) as text if resultFile does not end with ".webloc" then set resultFile to resultFile & ".webloc" tell application "Finder" make new internet location file at filePath to theURL with properties {name:fileName} end tell end alfred_script on replace_chars(this_text, search_string, replacement_string) set AppleScript's text item delimiters to the search_string set the item_list to every text item of this_text set AppleScript's text item delimiters to the replacement_string set this_text to the item_list as string set AppleScript's text item delimiters to "" return this_text end replace_chars on lastpos(textstring, thischar) return (length of textstring) - (offset of thischar in (reverse of text items of textstring as string)) end lastpos Link to comment
RodgerWW Posted February 6, 2014 Share Posted February 6, 2014 I can get this to work by moving two 'sets' after the dialog as such: on alfred_script(q) set defaultPath to "SYSTEM:Users:fns:Dropbox:URL" as alias tell application "Safari" to set theName to name of front document tell application "Safari" to set theURL to URL of front document set theName to replace_chars(theName, ":", " -") set text item delimiters to ":" set resultFile to (choose file name with prompt "Save URL" default name theURL default location filePath) as text if resultFile does not end with ".webloc" then set resultFile to resultFile & ".webloc" set fileName to last text item of resultFile set filePath to text 1 thru lastpos(resultFile, ":") of resultFile tell application "Finder" make new internet location file at filePath to theURL with properties {name:fileName} end tell end alfred_script on replace_chars(this_text, search_string, replacement_string) set AppleScript's text item delimiters to the search_string set the item_list to every text item of this_text set AppleScript's text item delimiters to the replacement_string set this_text to the item_list as string set AppleScript's text item delimiters to "" return this_text end replace_chars on lastpos(textstring, thischar) return (length of textstring) - (offset of thischar in (reverse of text items of textstring as string)) end lastpos On MY system, I changed the default path variable ... but, Loaded a site in Safari, triggered the script and it saved the webloc into my documents folder with the dialog popping up in front of Safari so I could tag it. Link to comment
fns720 Posted February 6, 2014 Author Share Posted February 6, 2014 Does not work for me in an Alfred workflow. It works from AppleScript editor but it still looses the focus if I start as an Alfred workflow. Link to comment
RodgerWW Posted February 6, 2014 Share Posted February 6, 2014 k, export the workflow maybe, and provide a link for me to test it here? Link to comment
fns720 Posted February 6, 2014 Author Share Posted February 6, 2014 https://www.dropbox.com/s/q4riiyirb7df0ki/Save%20URL.alfredworkflow Link to comment
RodgerWW Posted February 6, 2014 Share Posted February 6, 2014 looks to me like you need to tell Safari to activate as the save dialog is from Safari Link to comment
fns720 Posted February 6, 2014 Author Share Posted February 6, 2014 (edited) Thanks Rodger, now it works: on alfred_script(q) set defaultPath to "SYSTEM:Users:fns:Dropbox:URL" as alias tell application "Safari" to set theName to name of front document tell application "Safari" to set theURL to URL of front document set theName to replace_chars(theName, ":", " -") tell application "Safari" activate set resultFile to (choose file name with prompt "Save .webloc File" default name theName default location defaultPath) as text -- if resultFile does not end with ".webloc" then set resultFile to resultFile & ".webloc" end tell set text item delimiters to ":" set fileName to last text item of resultFile set filePath to text 1 thru lastpos(resultFile, ":") of resultFile tell application "Finder" to make new internet location file at filePath to theURL with properties {name:fileName} end alfred_script on replace_chars(this_text, search_string, replacement_string) set AppleScript's text item delimiters to the search_string set the item_list to every text item of this_text set AppleScript's text item delimiters to the replacement_string set this_text to the item_list as string set AppleScript's text item delimiters to "" return this_text end replace_chars on lastpos(textstring, thischar) return (length of textstring) - (offset of thischar in (reverse of text items of textstring as string)) end lastpos Edited February 6, 2014 by fns720 Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now