So I've been trying to write an AppleScript script that would create a folder with an input of user, but unfortunately I can't get it working. Any help is appreciated.
set theResponse to display dialog "Enter the name of folder" default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue"
display dialog "Name of the folder: " & (text returned of theResponse) & "."
tell application "Finder"
make new folder at desktop with properties {name:"theResponse"}
end tell
Here's a modified version of your code that will work:
set theResponse to text returned of (display dialog "Enter the name of folder" default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue")
display dialog "Name of the folder: " & theResponse & "."
tell application "Finder"
make new folder at desktop with properties {name:theResponse}
end tell
In your original code, {name:"theResponse"}, with "theResponse" in quotes, it's literally theResponse not the text returned.
So, In the first line of code, I started off with setting the theResponse variable to the text returned, so it need not be referenced as such later in the script.
Thus (text returned of theResponse) in the second line of code is now set to just theResponse, which contains the value of that variable.
Now when it comes time to make new folder the name property is the value of theResponse, without quotes, and it already contains the text returned from the first line of code.
Related
I'm new to Applescript. I am stumped on this problem of how to read and write a text file and use the contents as a variable. I have done research on this, but nothing works or makes sense. I want to read a text file, which would contain a word or numbers. The word or numbers, let's say 123, would be assigned to a variable called pass. I need an Applescript to ask the user what the password should be, then make a new text file with the password on it. I also need an Applescript to change the password. The following Applescript will be for changing the password.
set theFile to "Users:username:Desktop:pass.txt" as alias
set pass to (read file theFile)
display dialog "Password:" default answer "" with hidden answer
if text returned of result is pass then
display dialog "New Password:" default answer "" with hidden answer
display dialog "Again:" default answer "" with hidden answer
-- code here to change text in pass.txt
display dialog "Password changed."
end if
I just need a starter, or a useful website, or anything that can help me. Thanks!
You can do this with plain Applescript Standard Additions. You find the handlers inside the File Read/Write section.
set theFile to "Users:Username:Desktop:pass.txt" as alias
set pass to (read theFile)
display dialog "Password:" default answer "" with hidden answer
if text returned of result is pass then
set newPass1 to text returned of (display dialog "New Password:" default answer "" with hidden answer)
set newPass2 to text returned of (display dialog "Again:" default answer "" with hidden answer)
-- check the new passwords
if newPass1 = newPass2 and newPass1 ≠ "" then
-- open the file
set pwdFile to open for access theFile with write permission
-- delete the content
set eof of pwdFile to 0
-- write new content
write newPass1 to pwdFile
-- close the file
close access pwdFile
display dialog "Password changed."
end if
end if
BTW: read theFile does not need a file, your alias theFile is enough!
BTW: I hope this script is for learning purposes only. I strongly recommend not to handle your user's passwords this way...!
Enjoy, Michael / Hamburg
I have a dialog which opens to input and store user input(name).
Upon error (which is is either due to no name entered or the name already exists) i want the dialog to re-open. Finally, if the second attempt fails again, open a dialog which states that and exit.
The problem is, whether the name exists or does not, all 3 dialogs are ALWAYS displayed.
What am i missing?
try
display dialog "Specify a new folder name:" default answer "John The Dog"
set newName to (text returned of result)
on error errorMessage number errorNumber
end try
try
display dialog "Specify a DIFFERENT folder name:" default answer "John The Dog12"
set newName to (text returned of result)
on error errorMessage number errorNumber
end try
try
display dialog "NAME ALREADY EXISTS! The program will now exit." with icon caution buttons {"EXIT"}
end try
Thanks!
Try this. Note I did not test this but it should work. Just put this code in place of your code where you try to get the folder name.
You can basically create as many dialogTexts/defaultAnswers combinations as you want and it will work. Good luck.
-- get the names of all the folders in the targetFolder
tell application "Finder" to set targetFolderNames to name of folders of folder targetFolder
set dialogTexts to {"Specify a new folder name:", "Specify a DIFFERENT folder name:"}
set defaultAnswers to {"John The Dog", "John The Dog12"}
repeat with i from 1 to count of dialogTexts
display dialog item i of dialogTexts default answer item i of defaultAnswers
set newName to (text returned of result)
if newName is not "" and newName is not in targetFolderNames then exit repeat
if i is (count of dialogTexts) then
display dialog "NAME ALREADY EXISTS! The program will now exit." with icon caution buttons {"EXIT"} default button 1
return input
end if
end repeat
-- do something with newName
While trying to make an automator action that opens multiple tabs from selected text as input I ran into an applescript issue I wasn't able to solve for awhile. This includes the answer and I'm posting this here because I just wasn't able to find documentation on how to handle data in "input" for a receives selected "text" in "any application" automator action, everything is for files which comes in as a list already.
When putting an applescript action in, you get:
on run {input, parameters}
the problem here is that input isn't in a list format and trying to do anything with it breaks the script or throws an error. ie I can't do:
repeat with URL in input
set this_URL to URL
So how can I treat a list of selected text as a list of items?
the solution is first treat input as a string then break apart every paragraph.
on run {input, parameters}
set inputText to input as string
set URL_list to every paragraph of inputText
Without treating input "as string" first before doing "every paragraph of" it won't work.
Here's the end working script, replace the "some_url" with your own. You'll be able to select several lines of text in an editor and treat each one as a parameter to your fixed url opening each in a new safari tab. This could be expanded upon by having each line be delimited for multiple params on the url.
on run {input, parameters}
set inputText to input as string
set URL_list to every paragraph of inputText
tell application "Safari"
activate
repeat with URL in URL_list
set this_URL to URL
# extra processing of URL could be done here for multiple params
my new_tab()
set tab_URL to "http://some_url.com?data=" & this_URL
set the URL of document 1 to tab_URL
end repeat
end tell
return input
end run
on new_tab()
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
click menu item "New Tab" of ¬
menu "File" of menu bar 1
end tell
end tell
end new_tab
As an example say you had the list and had a service of the above using "http://stackoverflow.com/posts/" & this_URL
6318162
6318163
6318164
you could now select them click services and choose your "StackOverflow - view questions" service and it'll append and open each one in a new safari tab. In my case I needed to verify multiple dns entries in our server as still valid and do a bunch of whois lookups.
I was looking for the same thing, just for files as input from Automator to AppleScript.
ddowns's trick didn't work for that, but ended up using this, hope it's helpful for someone looking for solving the same issue I ran into:
on run {input, parameters}
-- create empty list
set selectedFiles to {}
-- add each list item to the empty list
repeat with i in input
copy (POSIX path of i) to end of selectedFiles
end repeat
-- show each item (just for testing purposes of course)
repeat with currentFile in selectedFiles
display dialog currentFile as text
end repeat
end run
As Hanzaplastique says, for AppleScript within Automator, you don't need the Safari AppleScript because there's an Action for that. I use the following Actions:
Extract URLs from Text (actually the 'Extract Data from Text' Action)
Run AppleScript
Display Webpages
I use it as a Workflow added to the Services menu so that I can right-click on selected text in an email and open multiple URLs in Safari tabs.
In particular, I get Server / WordPress updates in an email but the URLs are just the top level of the domains and I want to jump to the plugins page of WordPress. So, my AppleScript (with thanks to Hanzaplastique) is:
on run {input, parameters}
set AppleScript's text item delimiters to {return & linefeed, return, linefeed, character id 8233, character id 8232}
-- create empty list
set selectedFiles to {}
-- add each list item to the empty list
repeat with i in input
set AppleScript's text item delimiters to {" "}
set i to i & "/wp-admin/plugins.php"
copy i to end of selectedFiles
end repeat
return selectedFiles
end run
I found I needed the 'return selectedFiles'. The always mysterious (to me) text delimiters may not be necessary and come from the previous version which only pulled out a single URL.
this script works fine until the commands of the buttons, can someone tell my why i doesent work?
it says "button_pressed is not definded"
display dialog "bla" with icon alias ((path to me) & "Contents:Resources:my.icns" as string) buttons {"blu", "bli", "blaa"} default button 3
if the button_pressed is "blu" then
-- action for 1st button goes here
say "blu"
else if the button_pressed is "bli" then
-- action for 2nd button goes here
say "bli"
else
-- action for 3rd button goes here
say "bla"
end if
The appropriate way to do this is to use button returned:
display dialog "bla" with icon alias ((path to me) & "Contents:Resources:my.icns" as string) buttons {"blu", "bli", "blaa"} default button 3
set theResponse to button returned of the result
if theResponse is "blu" then
-- action for 1st button goes here
say "blu"
else ...
The error occurs because the variable button_pressed is NOT defined. All you have to do is add this line of code before the if block and it should work!
set the button_pressed to the button returned of the result
Variables (i.e. button_pressed) must ALWAYS be defined before they can be uesd. For example, this code will not function...
display dialog greeting --> ERROR
...while this one will:
set greeting to "Hello! I am now a defined variable!"
display dialog greeting
I have followed this youtube tutorial: http://www.youtube.com/watch?v=0ux3S_G8HuU
I want the second button go to a special website....
I have tried this:
if "button 2" then open location "(the name of the site)"
What do I do? I hope you can help.
Try this:
set search to (display dialog "Enter YouTube Video Search" default answer "" buttons {"Cancel", "Search", "Search2"} default button 3)
set keyword to text returned of search
set buttonPressed to button returned of search
if buttonPressed is equal to "Search" then
tell application "Safari"
open location "http://www.youtube.com/results?search_query=" & keyword
end tell
else if buttonPressed is equal to "Search2" then
tell application "Safari"
open location "http://www.youtube.com/results?search_query=" & keyword
end tell
end if
Just change the URLs to whatever you want for Search or Search2.