Applescript: Textedit Password - macos

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

Related

Automator "copy finder object" with AppleScript

I have created an Apple Automator service that
receives PDFs
encrypts them
copies the encrypted PDF to a destination folder
renames the encrypted PDF
These are all commands from the Automator. However, the copying as an Automator command can only copy to a pre-defined folder.
I would like to control this part by an AppleScript which reads out the user name and selects a folder accordingly:
on run {input, parameters}
set user_script to "echo $USER"
set username to do shell script user_script
if username = "A" then
set standardpfad to "/Users/" & username & "/whatever"
else if username = "B" then
set standardpfad to "/Users/" & username & "/foo"
else
display dialog "I don't know this user!" with title "ERROR" buttons {"OK"} default button "OK"
return
end if
#actual copying
end run
Unfortunately, I don't know how to handle the input in way that it resembles the "Copy Finder object" command in Automator. Can anyone please help me?
Thank you!
Edit:
Automator screenshot
Going by your original script, which seems to want to move all the files to the user's home folder, you can accomplish what you want using an automator variable. First, go to the upper left of the Automator window, click on the tab button that says 'Variables', then click on the 'Locations' item. Look for the item that says 'Home' (I believe that's 'Privat' on your machine's language):
This provides to a path to the user's home folder, for whichever user is running the workflow (system and machine independent). Drag this variable over to the Copy Finder Items (Finder-objekte kopieren) action, and drop it on the 'To:' ('Nach:') pull-down menu. It should look like this:
That should do the trick.
There are an assortment of system defined user paths you can choose from. You can also define a custom one using the special 'Text' variable (under 'Text & Data'), typing a path in standard unix notation where the tilde ('~') represents the user's home folder: e.g., ~/path/to/Custom Folder/.
If you're doing something more complicated and really need to use a Run AppleScript action, all you need to know is that the list of files is passed into the action in the input variable as a list of aliases, and whatever you return (should be a list of aliases or posix paths), will be passed on to the next action. For example:
on run {input, parameters}
set output to {}
repeat with this_item in input
set new_item to this_item -- ... obviously you'd do something other than just copy
copy new_item to end of output
end repeat
return output
end run
But it doesn't seem like you need to do that here; the special Automator variables should get you where you're going.
EDIT
Per comments, here's a revised version of the workflow...
Add the following actions to the workflow given in the question in place of the "Copy Finder Objects" action. Note that the second and sixth actions are set to ignore input from the previous action. These actions do the following:
Save the list of files to be copied to a storage variable called 'FileToCopy'; pass no data on
Get the path to the user's home folder; pass it to the next action
Get the user's 'user name'; pass the home folder and user name to the next action as a list
Run an AppleScript that constructs a unix path string from the input list; pass the completed path string on to the next action
Save the path string into a variable called 'DestinationFolder'; pass no data on
Retrieves the list of files to copy we saved in step #1; pass it on to the next action
Copy the files to the selected folder, using the 'DestinationFolder' variable we saved in step #5; pass these on to the Rename Finder Items action (not shown here)
Give it a go, and let me know how it works.

Script editor creating a folder and naming it

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.

On error re-open last dialog

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

AppleScript password checking

I'm trying to get simple user authentication going via AppleScript. The goal is to have the passwords check against each other and then continue on with the rest of the script. Right now the script can recognize mis-matched password if password_initial is correct and password_final is incorrect, but if password_initial is incorrect and password_final is correct there is no logic to check against itself.
set user_name to ""
display dialog "Please enter your user name" default answer "firstname.lastname"
set the user_name to the text returned of result
set password_initial to ""
display dialog "Please enter your password:" default answer "" with hidden answer
set password_initial to the text returned of result
display dialog "Please verify your password below." buttons {"OK"} default answer "" with hidden answer
set password_final to text returned of result
considering case
repeat while password_final is not equal to password_initial
display dialog "Passwords do not match, please re-enter your password below." buttons {"OK"} default answer "" with hidden answer
set password_final to text returned of result
end repeat
end considering
--do something
Can anyone point me in the right direction on this? Thanks!
Whoa whoa whoa, handlers and global variables? No need for all that, why not just throw the whole thing into a loop and then break it when we get what we want?
set user_name to text returned of (display dialog "Please enter your user name" default answer "firstname.lastname")
set display_text to "Please enter your password:"
repeat
considering case
set init_pass to text returned of (display dialog display_text default answer "" with hidden answer)
set final_pass to text returned of (display dialog "Please verify your password below." buttons {"OK"} default button 1 default answer "" with hidden answer)
if (final_pass = init_pass) then
exit repeat
else
set display_text to "Mismatching passwords, please try again"
end if
end considering
end repeat
#Rest of script here...
The trick with something like this is to use Handlers.
These are bits of code that can be called to run within your script as many times as you want and save on rewriting the same code over again.
They can also help you not use repeat loops like you are doing. Also you should always add the 'cancel' button to your display dialogs. If there is bad logic in a repeat loop or a handler call the user needs a way to exit it.
I have also made some of the display dialog text dynamic. and used some global variables
I have tested this code which has Handles and the calls to them and it works well in my testing. But it is an example and should give you enough to move forward.
set displays to {"Please enter your password:", "Please verify your password below.", "Passwords do not match, please re-enter your password below."}
set user_name to add_user_name() #get user name
set thedisplay to item 1 of displays #set the fisrt dialog for the password display to the first item in displays
global thedisplay, displays, password_initial #global variables
set password_final to setDetails() #Call to start the password dialogs
--your code here ..
#HANDLERS
on setDetails()
set password_initial to add_password() #call to get user password
set password_final to verify_password() #call to get verify password
end setDetails
on add_user_name()
display dialog "Please enter your user name" buttons {"Cancel", "OK"} default answer "firstname.lastname"
set the user_name to the text returned of result
return user_name
end add_user_name
on add_password()
display dialog thedisplay buttons {"Cancel", "OK"} default answer "" with hidden answer
set password_initial to the text returned of result
return password_initial
end add_password
on verify_password()
set thedisplay to item 2 of displays #set the dialog for the password verify display to the second item in displays
display dialog thedisplay buttons {"Cancel", "OK"} default answer "" with hidden answer
set password_final to text returned of result
considering case
if password_final is not equal to password_initial then
set thedisplay to item 3 of displays #set the dialog for the password verify display to the third item in displays
my setDetails() # start over again asking for password as it did not does not match dialog displays will also change accordingly
else
set thedisplay to item 2 of displays #set the dialog for the password verify display to the second item in displays
end if
end considering
return password_final
end verify_password

Weird behavior from the results of "Get Link URLs from Webpages" action

I'm building an Automator workflow that parses a Dropbox "gallery" page containing Quicktime video files, and automatically builds "direct-download" links for each of the files. In the end, I want all the links to be generated into the body of a Mail.app message.
Basically, everything works as expected, except for the part where the new links are sent to a new Mail.app message. My problem is that the links are sent to the body of the message without newlines, so they all get concatenated into a single line, like this:
https://dl.dropbox.com/u/149705/stackexchange/20121209/stackexchange_automator_prob2.png
(Mail.app seems to be wrapping the concatenated string on the question-marks)
The oddest thing about this is that if I use a "Copy to Clipboard" action at the end of the workflow (instead of my send-to-Mail Applescript), I get totally different results when I paste the same clipboard contents into TextEdit vs. BBEdit.
The links seem to paste into TextEdit properly. However, the same clipboard, pasted into a plaintext BBEdit document, yields only the first link:
https://dl.dropbox.com/u/149705/stackexchange/20121209/stackexchange_automator_prob5.jpg
What could be causing these 3 entirely different behaviors from the exact same results of the "Get Link URLs" action?
The reason you get that result is it the links start out as a List of strings but are being sent to mail.app as a single string.
You do not need to do anything fancy to fix this just put a return after the link.
I would just use this a single 'Run applescript' Action to do the whole job.
The script gathers only the file DL links. Adds a return on the end and send them to Mail.app.
No other formatting required
on run {input, parameters}
set db_tag to "dl-web.dropbox.com/get"
set myLinks to {}
tell application "Safari"
set thelinkCount to do JavaScript "document.links.length " in document 1
repeat with i from 1 to thelinkCount
set this_link to (do JavaScript "document.links[" & i & "].href" in document 1) as string
if this_link contains db_tag then
--add the link with a carriage return on the end.
copy this_link & return to end of myLinks
end if
end repeat
end tell
tell application "Mail"
activate
make new outgoing message with properties {visible:true, content:"" & myLinks}
end tell
end run
Get rid of all of the other actions and update myPage with your gallery URL.
on run
set myPage to "https://www.dropbox.com/sh/aaaaaaaaaaaaaaa/aaaaaaaaaa"
set myLinks to do shell script "curl " & quoted form of myPage & " | grep -Eo 'https://www.dropbox.com/sh/[^/]*/[^/\"]*/[^\"]*' | sed s'/https:\\/\\/www.dropbox.com\\(.*\\)/https:\\/\\/dl.dropbox.com\\1?dl=1/g'"
tell application "Mail"
activate
make new outgoing message with properties {visible:true, content:"" & myLinks}
end tell
end run
Each application is different, it understand or don't understand the output's format.
Regarding your AppleScript action:
input is an AppleScript list, the coercion from a empty string "" do a single line, because the delimiters is set to "" by default.
To get the desired result, set the delimiters to return or linefeed, like this.
on run {input}
set {oTID, text item delimiters} to {text item delimiters, linefeed}
set tContent to input as text
set text item delimiters to oTID
tell application "Mail"
activate
make new outgoing message with properties {visible:true, content:tContent}
end tell
return input
end run

Resources