Web search for filename using Automator - macos

I'm attempting to formulate an Automator script that takes a filename from Finder and searches for it on a specific website using Google.
It looks like I'm getting the filename right and can put it in a variable, but I can't figure out if there is a way to pass the variable to the search URL. There is no variable input field for URLs. Can perhaps some intermediate function be used?

You can use this script, it return an URL (no need for other actions or variables):
on run {input, parameters}
set tName to name of (info for (item 1 of input) without size)
return "https://www.google.com/search?q=" & tName & "%20site%3Amywebpage.com"
end run

Related

Using automator and AppleScript to move a file to a different folder and subfolder based on name and content of a file

I'm a total newbie on Automator and Scripting...
I have read a lot of answers to problems a bit similar to mine, but I don't succeed to adapt with Automator + AppleScript.
Here is what I want to do:
When I download a file to a directory /Volumes/Macboot /Downloads, (yes there is a space in the HDD's name), e.g. statement_EUR_2020-05-01_2020-05-31.pdf.
I verify if the file is with extension pdf + it contains an IBAN + the name contains "statement".
If the file corresponds, I want to verify the year and month in the name and move it accordingly to the good Google Drive folder:
/Volumes/Macboot /Travail en cours/Google Drive/Company/Comptabilité/**2020**/**05**/Compte Transferwise 1/
Right now, I succeeded to obtain year and month in 2 variables, but I can't find a good way to move the file using variables in the next step in Automator.
The following should work, or at least get you on the correct path. Set the following for variables:
monthName: a text variable that holds the month value, as above
yearName: a text variable that holds the year value, as above
filePath: a storage variable that holds reference to the file you are working on
outputFolder: a storage variable that will hold a reference to the output folder
The first two actions collect the month and year from storage and pass it to the AppleScript action as a list in the input variable. That AppleScript action extracts the values from the list, concatenates them into a path string, and the uses the POSIX File command to turn that into a file reference. The fourth action stores the file reference in the outputFolder variable.
Note that the fifth action ignores the input from the fourth action. Instead, it recovers the original file specifier (that you will have stored somewhere earlier in the workflow, then sends the file specifier to the Move Finder Items actions, which uses the value stored in the outputFolder variable as its destination.
I've simplified the process and found a way with AppleScript:
on run {input, parameters}
set theFile to input as text
set yearName to ((characters 34 thru -1 of theFile) as string) --trim first 35
set yearName to ((characters 1 thru -22 of yearName) as string) --trim last 23
set monthName to ((characters 39 thru -1 of theFile) as string)
set monthName to ((characters 1 thru -19 of monthName) as string)
set destinationFolder to ("Macboot :Travail en cours:Google Drive:Company:Comptabilité:" & yearName & ":" & monthName & ":Compte Transferwise 1:Relevé PDF + fichier CSV:" as text)
tell application "Finder"
activate
move theFile to destinationFolder -- use "copy source_file to folder (target_folder as alias)" to copy the files
end tell
set result to {yearName, monthName, theFile, destinationFolder}
return result
end run

Change the variable in a workflow from a separate workflow

I have two workflows and I need to pass a value generated in one workflow to another workflow.
In my first workflow, I have an AppleScript which returns a number I want to put into a second workflow which I call from the first workflow like so:
My second workflow (Create Class in iStudiez) has a variable 'Class Number' which I want to change when I call it from my first workflow with the return value of the AppleScript pictured above.
Since you are using Automator and AppleScript in Automator, and you have not posted any actual code, it's difficult to give you an exact answer of what you're looking for.
There may be an easier solution but the solution I came up with was to create one script which will save a variable into a new script file (which will automatically be created on your desktop with the name of “Stored_Variable.scpt”. The second script loads the value of the variable stored in the “Stored_Variable.scpt” file.
Simply paste the code from this first script, directly into the code which contains the variable you want to copy. Be sure to paste the code after the code which sets the value of the variable you want copied.
-- Comment Out This Next Line Before
-- Placing This Code Into Your Script
-- Which Contains The Variable You Want Copied
set originalVariable to (path to desktop) -- Testing Purposes Only
-- Replace "originalVariable" with the
-- Name Of Your Actual Variable You Want To Pass
-- To The Next Script
set saveThisVariable to originalVariable
storeTheVariable()
-- The Following Code Belongs At The Very Bottom Of Your Script
on storeTheVariable()
set storedVariabeFileLocation to (path to desktop as text) & "Stored_Variable.scpt"
----------------------
script theVariable
set saveThisVariable to saveThisVariable
end script
----------------------
store script theVariable in ¬
file storedVariabeFileLocation with replacing
end storeTheVariable
Place this is second code inside the code of your AppleScript in which you are trying to retrieve the variable stored from the first AppleScript code
-- Gets The Variable Which Was Previously Stored
-- From The Other Applescript And Stores It In A
-- New Variable... getVariableNow
set getVariableNow to run loadTheVariable
-- -----------------------------------
-- Place Whatever Commands Here, That You Will Be Using
-- The New Variable... getVariableNow with
-- -----------------------------------
-- The Following Code Belongs At The Very Bottom Of Your Script
script loadTheVariable
property storedVariabeFileLocation : (path to desktop as text) & "Stored_Variable.scpt"
property theRetrievedVariable : missing value
on getStoredVariable()
set theScript to load script file storedVariabeFileLocation
set theRetrievedVariable to saveThisVariable of (theVariable of theScript)
end getStoredVariable
set theRetrievedVariable to loadTheVariable's getStoredVariable()
end script

Applescript Choose file, filename

What I'm trying to make is something like this:
set filename to (choose file)
display dialog "The name of the chosen file is: " & filename
But when I use that script it will return a path to the file something like .../.../.../.../nameoffile.jar. But what I want is that it will return the filename and not the path (so just only nameoffile.jar). Is there a way to do that?
Thanks for reading, please give an answer as soon as possible ;),
Jort
Use the Finder Suite:
set chosenfile to (choose file)
tell application "Finder"
display dialog "The name of the chosen file is: " & (name of chosenfile)
end tell

Updating script to find a new type of file

I need to update the script shown below to find a new file type and naming system and have no idea what I'm doing. It use to pull a file named DQXXXXX1.eps and placed in the listed location. The new files are XXXXX_random.pdf. The random in the file name is several series of numbers that change for each file. The important numbers are the first 5, I would like the script to pull all files in that initial location and place into the other location.
The current script is:
set DQfolder to alias "Prepress:ArtFiles:00-Logos A to Z:1-DQ:"
tell application "Finder"
display dialog "enter number" default answer ""
set theNum to text returned of result as string
--try
move alias (DQfolder & "DQ" & theNum & ".eps" as string) to "Macintosh HD:__DQ Incoming:" with replacing
--on error {}
--move alias (DQfolder & "DQ" & theNum & ".tif") to "Macintosh HD:__DQ Incoming:" with replacing
--end try
end tell
Try your script like the following. You have a few things that need to be fixed...
Basically you are adding the strings wrong. You need to first coerce DQfolder to a string before you can add the other strings to it. The way you are doing it can't work because DQfolder is an alias so you can't add other strings to it until you coerce the alias to a string.
the folder path in your command has to be an alias-type file or a folder specification. As such you need to put the word "alias" or "folder" in front of it. A string path will not work. Applescript rarely works with string paths.
"display dialog" is not a Finder command and as such you don't need to put it in the Finder block of code.
"text returned" from your display dialog command is already "text" so you do not need to coerce it to a string. That's why it's called "text" returned... even if you entered a number it's class is still text.
Of course I can't check this code because I don't have files at those paths, but it should work. Good luck...
set DQfolder to alias "Prepress:ArtFiles:00-Logos A to Z:1-DQ:"
display dialog "enter number" default answer ""
set theNum to text returned of result
tell application "Finder"
move alias ((DQfolder as text) & "DQ" & theNum & ".eps") to alias "Macintosh HD:__DQ Incoming:" with replacing
end tell

Getting the file name of files dropped on the script

I made this Applescript script to create symbolic links.
Appart from POSIX path of, how can I get the file name, without the path, of the dropped file?
on open filelist
repeat with i in filelist
do shell script "ln -s " & POSIX path of i & " /Users/me/Desktop/symlink"
end repeat
end open
PS: I know this expects many files to be dropped and tries to create many links with the same name, which gives an error. Actually I copied this example from a website and as I don't know almost anything about Applescript, I don't know how to do this for a single file, help on that would be appreciated too.
I'm not sure what precisely you're trying to do, but I have a guess. Is the idea that you want to take every file dropped on the script and create a symbolic link to each one on the Desktop? So if I drop ~/look/at/me and ~/an/example, you'll have ~/Desktop/me and ~/Desktop/example? If that's what you want, then you're in luck: ln -s <file1> <file2> ... <directory> does exactly that. (Edit: Although you have to watch out for the two-argument case.) Thus, your code could look like this:
-- EDITED: Added the conditional setting of `dest` to prevent errors in the
-- two-arguments-to-ln case (see my comment).
on quoted(f)
return quoted form of POSIX path of f
end quoted
on open filelist
if filelist is {} then return
set dest to missing value
if (count of filelist) is 1 then
tell application "System Events" to set n to the name of item 1 of filelist
set dest to (path to desktop as string) & n
else
set dest to path to desktop
end if
set cmd to "ln -s"
repeat with f in filelist & dest
set cmd to cmd & " " & quoted(f)
end repeat
do shell script cmd
end open
Note the use of quoted form of; it wraps its argument in single quotes so executing in in the shell won't do anything funny.
If you want to get at the name of the file for another reason, you don't need to call out to the Finder; you can use System Events instead:
tell application "System Events" to get name of myAlias
will return the name of the file stored in myAlias.
Edit: If you want to do something to a single file, it's pretty easy. Instead of using repeat to iterate over every file, just perform the same action on the first file, accessed by item 1 of theList. So in this case, you might want something like this:
-- EDITED: Fixed the "linking a directory" case (see my comment).
on quoted(f)
return quoted form of POSIX path of f
end quoted
on open filelist
if filelist is {} then return
set f to item 1 of filelist
tell application "System Events" to set n to the name of f
do shell script "ln -s " & ¬
quoted(f) & " " & quoted((path to desktop as string) & n)
end open
It's pretty much the same, but we grab the first item in filelist and ignore the rest. Additionally, at the end, we display a dialog containing the name of the symlink, so the user knows what just happened.
As an example, you can work with the Finder instead of a shell script to get the name of a single file that is dropped on the script that is saved as an application. If you don't need the display dialog, you can remove it, but you have the file name as a variable to work with:
on open the_files
repeat with i from 1 to the count of the_files
tell application "Finder"
set myFileName to name of (item i of the_files)
end tell
display dialog "The file's name is " & myFileName
end repeat
end open

Resources