How to assign (file.txt) tu variable via Applescript - applescript

I need to select a .txt file that contains a list of urls and pass it to theFile variable.
unfortunately it doesn't work, the system says it can't find the file.
on run
set theFile to ("Macintosh HD⁩:⁨Utenti⁩:enricosindico⁩:Scrivania:URL.txt⁩")
open theFile

Related

Using applescript to select a folder and use as variable

I need to modify my current code to allow for the selection of a a folder. My current code is as follows and works to select a folder without issue:
tell application "Finder"
set sourceFolder to folder POSIX file "/Users/Username/Desktop/Upload/Temp/HighRes/"
set theFiles to files of sourceFolder
set inputPath to "/Users/Username/Desktop/Upload/Temp/"
end tell
I have tried the following, but cannot determine the correct syntax to get to where I was in my original code
tell application "Finder"
set inputPath to folder POSIX file (choose folder with prompt "Please choose folder to be processed")
set sourceFolder to folder POSIX file (inputPath & "/HighRes")
set theFiles to files of sourceFolder
end tell
The above processes but errors out and says that finder got an error and cannot make alias "xyz" into type integer.
Please forget the POSIX dance. The Finder likes its native and alias specifiers.
tell application "Finder"
-- inputPath is an alias specifier
set inputPath to (choose folder with prompt "Please choose folder to be processed")
-- sourceFolder is built as a Finder item specifier
set sourceFolder to folder "HighRes" of inputPath
set theFiles to files of sourceFolder
end tell
Even your first snippet can be written in a simpler way
tell application "Finder"
set sourceFolder to folder "Upload:Temp:HighRes" of desktop
set theFiles to files of sourceFolder
set inputPath to folder "Upload:Temp" of desktop
end tell

Applescript move all files from a specific folder name.

I have a drive that has multiple "Approved" folders with files in them.
The Approved folders are in various locations on the drive.
I need to move the contents of the Approved folders to another directory via applescript.
This is what I have come up with so far but does not seem to be doing the trick, it runs but no files are moved...
Any tips would be great
Thanks
set sourceFolder to "THIS:" as alias
set destinationFolder to "THAT:" as alias
tell application "Finder"
repeat with aFolder in (get folders of sourceFolder)
set folderName to name of aFolder
set filesToMove to (files of sourceFolder whose name = "Approved")
move filesToMove to destinationFolder
end repeat
end tell
According to your description, you are looking for folders named Approved, but your script is searching for files named Approved. Try this:
set sourceFolder to choose folder
set destinationFolder to choose folder
tell application "Finder"
repeat with aFolder in (get folders of sourceFolder)
if aFolder's name is "Approved" then
set filesToMove to (files of aFolder)
move filesToMove to destinationFolder
end if
end repeat
end tell

AppleScript Overwrite or Skip Command?

I have the following applescript that takes files and puts them in their corresponding folders.
set sourceFolder to choose folder
tell application "Finder"
set theFiles to files of sourceFolder
repeat with aFile in theFiles
set fileName to name of aFile
if fileName contains "#" then
set poundOffset to offset of "#" in fileName
set folderName to text 1 thru (poundOffset - 2) of fileName
set newFolder to (sourceFolder as text) & folderName & ":"
if not (exists folder newFolder) then
make new folder at sourceFolder with properties {name:folderName}
end if
move aFile to folder newFolder
end if
end repeat
end tell
It works great for me except in the case of a file conflict. If there's a file with the same name in the folder that it's putting it in the script gets an error and crashes. So my question is this....How do I fix that? I'm willing for it to just overwrite the file but is there a way to bring up a prompt to skip the file or just skip it all together and move on to the next one?
I'm a little fuzzy on what I can do here. Thanks in advance for the help.
Ringslinger.
Try:
move aFile to folder newFolder replacing true

AppleScript to compress a folder without folder structure?

I am trying to zip the entire files inside a folder using AppleScript. I use this code,
tell application "Finder"
set x1 to (path to home folder as text)
set theItem to x1 & "Documents:MyFolder" as alias
set itemPath to quoted form of POSIX path of theItem
set fileName to name of theItem
set theFolder to POSIX path of (container of theItem as alias)
set zipFile to quoted form of (theFolder & fileName & ".zip")
do shell script "zip -r " & zipFile & " " & itemPath
end tell
This works OK. But it zips the entire folder structure like this inside the zip file,
Users:MyUser:Documents:MyFolder:
I want to just zip the entire files inside the folder without this folder structure. I mean not even the MyFolder. Just the files inside to a zip file.
?
Here's a quick modification that will create zip all of the files inside of itemPath and output them to zipFile:
tell application "Finder"
set x1 to (path to home folder as text)
set theItem to x1 & "Documents:MyFolder" as alias
set itemPath to quoted form of POSIX path of theItem
set fileName to name of theItem
set theFolder to POSIX path of (container of theItem as alias)
set zipFile to quoted form of (theFolder & fileName & ".zip")
do shell script "cd " & itemPath & ";zip -r " & zipFile & " *"
end tell
If you run this as is, it will create MyFolder.zip in Documents containing the contents of MyFolder.
The trick going on here is the script is cd-ing into the folder first, and then recursively call zip on *, which represents all of the files in the current directory (which is the target to be zipped).

AppleScript to create aliases of files in a folder

So I have this AppleScript for making aliases of all the contents of one folder to another folder.
I works perfectly unless there is only one item in the source folder.
tell application "Finder"
set SourceFolder to (choose folder with prompt "Please choose Source folder:") as string
set DestinationFolder to (choose folder with prompt "Choose Destination folder for aliases:") as string
set theFolders to every item of entire contents of folder SourceFolder
repeat with thisFolder in theFolders
set thisName to name of thisFolder
make new alias file at folder DestinationFolder to thisFolder without invisibles
end repeat
end tell
Any idea why it's not getting anything when there's one item only? When there's at least 2 items in the source folder it creates aliases for both in the destination.
On a side note, any way to get it to remember the source and destination folders between times you run it?
Try:
property SourceFolder : missing value
property DestinationFolder : missing value
if SourceFolder = missing value then
set SourceFolder to (choose folder with prompt "Please choose Source folder:")
set DestinationFolder to (choose folder with prompt "Choose Destination folder for aliases:")
else
tell application "Finder"
set theFolders to every item of entire contents of SourceFolder as list
repeat with thisFolder in theFolders
make new alias file at DestinationFolder to thisFolder
end repeat
end tell
end if

Resources