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.
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 want to create new TXT file, but for some reason TXT file do not appears anymore when I click with right button on desktop, and go to 'NEW' (list of file types to create).
I saw these instructions somewhere else. They were really useful for me.
To Add an Item in “New” menu:
Type regedit in RUN dialog box and press Enter. Now expand “HKEY_CLASSES_ROOT” key.
Now look for the file type which you want to add in “New” menu, e.g. for adding MP3 file type look for .MP3 key.
Right-click on it and select “New -> Key” and give it name “ShellNew”.
In right-side pane, right-click and select “New -> String Value”. Give it name “NullFile” and press Enter.
Thats it. You’ll immediately get the file type entry in “New” menu.
Detailing:
Go to regedit
Open "HKEY_CLASSES_ROOT"
Find '.txt' and click on it
Check if "(Default)" "Data" value is "textfile", if not, set it to "textfile"
Check if "ShellNew" path exists in '.txt', if not, create it
Check if "NullFile" string value exists in "ShellNew", if not, create it
Find 'textfile' on "HKEY_CLASSES_ROOT" , if not exists, create it and set "(Default)" "Data" value to "Text Document" or other than you prefer
Go to desktop / right mouse click / Refresh to see your new item in 'New' list
Basically 4 things are needed:
.txt path with default value set as 'textfile'
ShellNew path into .txt path
'NullFile' string value into ShellNew with no data
textfile path with default value as the description of file
Try using this post http://www.mediacollege.com/microsoft/windows/extension-change.html.
This should show you how to make a text file.
If this doesn't work you can add it in to the context menu by following these instructions
https://superuser.com/questions/629813/create-new-text-document-option-missing-from-context-menu
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
Is there a way to create a script that will change the preferences of iChat to run the script when a message is received?
In other words, I want to make a script to change the iChat preferences to enable the "Message Received.applescript" i have created. Wow, this is confusing. Let me simply this.
I want a script that does this:
Activate iChat
Open iChat Preferences
Move to "Alerts" tab
select event "Message Received"
turn on "Run applescript"
select a certain script from the script folder called "Message Receive.applescript"
please help?
You just need to edit iChat's plist file. defaults is a bit awkward for nested values so use System Events instead:
tell application "System Events"
set EventActions to property list item "EventActions" of property list file ((path to preferences folder from user domain as text) & "com.apple.iChat.plist")
repeat with e in {property list item "MessageNotification" of EventActions, property list item "SubsequentMessage" of EventActions}
make new property list item at end of e with properties {name:"RunAppleScript", value:true}
make new property list item at end of e with properties {name:"iChatAppleScriptsKey", value:{"~/Library/Scripts/iChat/Message Received.scpt"}}
end repeat
end tell
This will attach "Message Received.scpt" to both initial text invitations and subsequent messages - you could simplify it if you only wanted it for subsequent messages. Also note you may need to relaunch iChat for the change to take effect.
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.