Jump to content

Focus on AppleScript "choose file name" dialog


Recommended Posts

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

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


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 by RodgerWW
Link to comment

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

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

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 by fns720
Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...