Applescript selection name extension - applescript

i got problem for the name extension of selection
it not work when i select more then 2 different files type
please help
property doc_list : {"pdf", "doc"}
property image_list : {"jpg", "png", "tif", "tiff", "gif"}
tell application "Finder"
set sel to (get selection)
repeat with AnItem in sel
end repeat
if sel = {} then
display alert ("nothing selected")
else if name extension of AnItem is in doc_list then
display alert ("this is doc files")
else if name extension of AnItem is in image_list then
display alert ("this is images files")
else if name extension of AnItem is in image_list & doc_list then
display alert ("this is images and doc files")
else if name extension of AnItem is not in image_list & doc_list then
display alert ("unknown file type")
end if
end tell

The end repeat line must be moved to before end tell.
This is a slightly improved version. The third else if clause will never be reached and the fourth can be just else
property doc_list : {"pdf", "doc"}
property image_list : {"jpg", "png", "tif", "tiff", "gif"}
tell application "Finder"
set sel to (get selection)
if sel = {} then
display alert ("nothing selected")
return
end if
repeat with AnItem in sel
set nameExtension to name extension of AnItem
if nameExtension is in doc_list then
display alert ("this is doc files")
else if nameExtension is in image_list then
display alert ("this is images files")
else
display alert ("unknown file type " & quote & nameExtension & quote)
end if
end repeat
end tell

Related

How do you add all photos in a folder to an Apple Photos album without importing any pictures?

This is useful when you have a folder of images that may contain images that you already have in your library.
on run
set folderList to (choose folder with multiple selections allowed)
tell application "Photos"
activate
delay 2
end tell
repeat with baseFolder in folderList
importFotos(baseFolder, "NEW ALBUM")
end repeat
end run
on importFotos(aFolder, albumName)
set imageList to getImageList(aFolder)
log (albumName)
if imageList is {} then return
set fotoAlbum to createFotoAlbum(albumName)
repeat with image in imageList
set fileInfo to info for image
set filename to name of fileInfo
set filenameStr to filename as string
log (filename)
tell application "Photos"
activate
set photosImage to search for filenameStr
add photosImage to fotoAlbum
log (photosImage)
end tell
end repeat
log (albumName)
end importFotos
on getImageList(aFolder)
set extensionsList to {"jpg", "png", "tiff", "JPG", "jpeg", "gif", "JPEG", "PNG", "TIFF", "GIF", "MOV", "mov", "MP4", "mp4", "M4V", "m4v", "MPG", "mpg", "BMP", "bmp", "TIF", "tif", "AVI", "avi", "PSD", "psd", "ai", "AI", "orf", "ORF", "nef", "NEF", "crw", "CRW", "cr2", "CR2", "dng", "DNG", "PEF", "HEIC"}
with timeout of (30 * 60) seconds
tell application "Finder" to set theFiles to every file of aFolder whose name extension is in extensionsList
end timeout
set imageList to {}
repeat with i from 1 to number of items in theFiles
set thisItem to item i of theFiles as alias
set the end of imageList to thisItem
end repeat
imageList
end getImageList
on createFotoAlbum(albumName)
tell application "Photos"
make new album named albumName
end tell
end createFotoAlbum
This is also available from my GitHub repository: https://github.com/vjsingh/make-album-without-importing

Add files to iTunes playlist

Sorry if it has already discussed but I'm unable to find an answer.
I would like to add files from a choosen folder to an existing iTunes' playlist.
I don't want to copy/duplicate these files (so iTunes would create a new folder inside its Music folder).
If I use:
add myFolder to playlist playListName
songs are duplicated in a new "Music" folder
If I use:
add myFile to playlist playListName
I have a type error, something about «class cUsP» id 20647 of «class cSrc» id 66 of application "iTunes".
My app code contains several subroutines, not easy to understand unless I post the entire code.
myFolder can be defined by dropping a folder onto the app, or by command choose folder.
Then, playListName is extracted from the choosen folder's pathname.
Then, folder is processed like this, in order to create playlist:
on processFolder(thisFolder, playListName)
tell application "iTunes"
activate
-------------------------------- create playlist
try
set newPlaylist to (make new user playlist with properties {name:playListName})
set view of browser window 1 to newPlaylist
on error errMsg
beep
my dlog("Unable to create playlist " & my kwote(playListName) & "." & return & return & errMsg, {"Abort"}, appTitle, "error")
return false
end try
--***add thisFolder to newPlaylist -- create new "Music folder" and duplicate files !!!
--------------- add files --> tracks
tell application "Finder"
repeat with thisFile in (get every file of folder thisFolder)
if (my isValidFile(thisFile)) then
tell application "iTunes"
try
add thisFile to playlist playListName -- ERROR !!!
on error errMsg
beep
my dlog("Unable to add file " & my kwote(thisFile as text) & "." & return & return & errMsg, {"Abort"}, appTitle, "error")
return false
end try
end tell
end if
end repeat
end tell
return true
end tell
end processFolder
on isValidFile(thisFile)
tell application "Finder"
set the fileInfo to (info for thisFile as alias)
if (mp3Check) then
if (alias of the fileInfo is false) and ((the file type of the fileInfo is in the typesList) or (the name extension of the fileInfo is in the extensionsList)) then return true
else
if (alias of the fileInfo is false) then return true
end if
return false
end tell
end isValidFile
Thank you.

Upload random file hourly with photos.app and applescript

I have baby photos in a folder and I want to upload them one at a time about every hour (4000 secs not 3600 secs) to a shared iCloud album that all my relatives see on their iPhones and iPads and macs. Here is my applescript saved as an application with the keep open box checked. I think it's not quite right. What's wrong?
on idle
set importFolder to "Amac:Users:AbuDavid:Downloads:uploadBABY"
set extensionsList to {"jpg", "png", "tiff"}
tell application "Finder" to set theFiles to some file of importFolder whose name extension is in extensionsList
if (count of theFiles) < 1 then
display dialog "No images selected!" buttons "OK"
else
set albumName to "BabyDouDou"
set timeNow to time string of (current date)
set today to date string of (current date)
set albumName to albumName & " " & timeNow & " " & today
set imageList to theFiles
tell application "Photos"
activate
delay 2
import imageList into albumName skip check duplicates yes
end tell
tell application "Finder" to move theFiles to trash
end if
return 4000
end idle
There are some issues:
The Finder needs the keyword folder – not just a literal string – to specify a folder.
some file returns always one file so the count command fails and returns always 0.
albumName is a literal string as well rather than a object specifier.
Photos.app expects alias specifiers for the files to be imported rather than Finder object specifiers.
Try this
on idle
set importFolder to (path to downloads folder as text) & "uploadBABY"
set extensionsList to {"jpg", "png", "tiff"}
tell application "Finder" to set theFiles to files of folder importFolder whose name extension is in extensionsList
if (count theFiles) < 1 then
display dialog "No images selected!" buttons "OK"
else
set theFile to some item of theFiles
set albumName to "BabyDouDou"
set timeNow to time string of (current date)
set today to date string of (current date)
set albumName to albumName & " " & timeNow & " " & today
set imageList to {theFile as alias}
tell application "Photos"
activate
delay 2
if not (exists container albumName) then
set theAlbum to make new album
set name of theAlbum to albumName
else
set theAlbum to container albumName
end if
import imageList into theAlbum skip check duplicates yes
end tell
tell application "Finder" to move theFiles to trash
end if
return 4000
end idle
Made a small change to only delete the image that was uploaded and not all the images. Thank you so much.
on idle
set importFolder to (path to downloads folder as text) & "uploadBABY"
set extensionsList to {"jpg", "png", "tiff"}
tell application "Finder" to set theFiles to files of folder importFolder whose name extension is in extensionsList
if (count theFiles) < 1 then
display dialog "No images selected!" buttons "OK"
else
set theFile to some item of theFiles
set albumName to "testscript"
set imageList to {theFile as alias}
tell application "Photos"
activate
delay 2
if not (exists container albumName) then
set theAlbum to make new album
set name of theAlbum to albumName
else
set theAlbum to container albumName
end if
import imageList into theAlbum skip check duplicates yes
end tell
tell application "Finder" to move theFile to trash
end if
return 7
end idle

Selecting multiple files in Finder

In my applescript, I have a function which takes a selected file and writes its path into an xml file. If a folder is selected, i'm able to create a list with the files contained and send that to my function. However, if I select multiple finder items (via shift clicking), whether files or folders, I'm not able to send anything to the function.
here is the part where i get the files from the Finder
tell application "Finder"
set myPath to the selection
end tell
--if multiple files selected
if (count of myPath) is greater than 1 then
set fileList to every item of myPath
repeat with i in fileList
if (isDirectory(i)) then
else
myBigLoop(initialSuccess, i, watchFolder)
end if
end repeat
else if (isDirectory(myPath)) then
submitFolder(myPath, watchFolder)
else
set isFolder to false
set initialSuccess to true
myBigLoop(initialSuccess, myPath, watchFolder)
end if
on myBigLoop(initialSuccess, fileList, watchFolder)
repeat with myPath in fileList
if initialSuccess then
tell application "Finder"
set myFilename to myPath as alias
set myPath to the folder of myFilename
set myPath to myPath as string
set myFilename to name of myFilename
display dialog myFilename
end tell
end if --end InitialSuccess
end repeat
end myBigLoop
on isDirectory(someItem) -- someItem is a file reference
set filePosixPath to quoted form of (POSIX path of (someItem as alias))
set fileType to (do shell script "file -b " & filePosixPath)
if fileType ends with "directory" then return true
return false
end isDirectory
on submitFolder(myPath, watchFolder)
set isFolder to true
set initialSuccess to true
set fileList to item 1 of myPath
set fileList to get every file of fileList
set numFiles to count fileList
if numFiles is equal to 0 then
display dialog "There were no files in that folder."
return false
end if
myBigLoop(initialSuccess, fileList, watchFolder)
end submitFolder
I believe you should extend the Finder tell structure, because you're still working with files and folders later on:
tell application "Finder"
set myPath to the selection
--if multiple files selected
if (count of myPath) is greater than 1 then
set fileList to every item of myPath
repeat with i in fileList
if (isDirectory(i)) then
else
myBigLoop(initialSuccess, i, watchFolder)
end if
end repeat
else if (isDirectory(myPath)) then
submitFolder(myPath, watchFolder)
else
set isFolder to false
set initialSuccess to true
myBigLoop(initialSuccess, myPath, watchFolder)
end if
end tell

Folder image to iTunes artwork in applescript : not working anymore

Anyone of you guys could point me to why this applescript (which was working on snow leopard) does not work anymore on 10.7 Lion.
It intend to copy the file named "folder.jpg" from each album folder into ID3 tags of selected mp3 in iTunes.
property tempfile : ((path to temporary items as string) & "itunespicturefile_temporaire.pict")
global vPictFolder
tell application "iTunes"
set v_TrackSelection to selection
if v_TrackSelection is not {} then
repeat with vTrack in v_TrackSelection
set vPictFolder to my (ParentFromPath(location of vTrack as string)) as text
set thisPict to my getpictData("folder.jpg")
if thisPict is not "" then
delete artworks of vTrack
set data of artwork 1 of vTrack to (thisPict)
end if
end repeat
else
display dialog ("select tracks first !")
end if
end tell
on getpictData(vAlbumpictName)
tell application "Finder" to tell file vAlbumpictName of folder vPictFolder to if exists then
set t_file to it as string
else
display dialog vPictFolder & vAlbumpictName
return ""
end if
do shell script "/opt/local/bin/convert " & quoted form of POSIX path of t_file & " " & quoted form of POSIX path of tempfile
display dialog "ok"
return read (tempfile as alias) from 513 as picture
end getpictData
on ParentFromPath(thePath)
set wantPath to true
set thePath to (thePath as text)
set saveDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set pathAsList to text items of thePath
if the last character of thePath is ":" then
set idx to (the number of text items in thePath) - 2
else
set idx to -2
end if
if wantPath then
set folderName to ((text items 1 through idx of pathAsList) as text) & ":"
else
set folderName to item idx of pathAsList
end if
set AppleScript's text item delimiters to saveDelim
return folderName
end ParentFromPath
Because Picture format (".pict") is not supported on Lion and newer OS.
You can use the term picture to read a image file (JPEG, PNG, ...), the format will not be in the Picture format , but the original format of the file.
tell application "iTunes"
set v_TrackSelection to selection
if v_TrackSelection is not {} then
repeat with vTrack in v_TrackSelection
set vPictFolder to my (ParentFromPath(location of vTrack as string)) as text
set thisPict to my getpictData(vPictFolder & "folder.jpg")
if thisPict is not "" then
delete artworks of vTrack
set data of artwork 1 of vTrack to (thisPict)
end if
end repeat
else
display dialog ("select tracks first !")
end if
end tell
on getpictData(tFile)
try
return (read (tFile as alias) as picture)
end try
display dialog tFile
return "" -- not exists
end getpictData

Resources