I need my AppleScript to find some data from a log (text) and set as a value
e.g Serial ID : XXXXXX , devices : XXXXX
Also the app is generating a lot of log files, so I need to take the data from the most recent logs.
Is that even possible
set listOfShows to {}
set Shows to paragraphs of (read POSIX file "Users/username/Library/Logs/app/Applicationc1454545.log.log")
repeat with nextLine in Shows
if length of nextLine is greater than 0 then
copy nextLine to the end of listOfShows
end if
end repeat
choose from list listOfShows/
Maybe I can get the file with this :
set sourceFolder to POSIX file "/Users/Users/Library/Logs/"
tell application "Finder"
sort (get files of folder sourceFolder) by creation date
-- This raises an error if the folder doesn't contain any files
set theFile to (item 1 of result) as alias
end tell
Actually when you sort by creation date in Finder the most recent file is the last rather than the first file.
The error occurs if there are no files and therefore no item 1.
Try this:
path to library folder from user domain is the relative path to the library folder of the current user.
set logFolder to (path to library folder from user domain as text) & "Logs:"
tell application "Finder" to set sortedFiles to sort (get files of folder logFolder whose name contains "Applicationc1454545") by creation date
if sortedFiles is not {} then
set mostRecentLogFile to last item of sortedFiles as alias
set listOfShows to {}
set Shows to paragraphs of (read mostRecentLogFile as «class utf8»)
repeat with nextLine in Shows
if length of nextLine is greater than 0 then
copy nextLine to the end of listOfShows
end if
end repeat
set chosen to choose from list listOfShows
if chose is false then return
set chosen to item 1 of chosen
end if
Related
I am attempting to read the contents of a txt file (list of IP addresses) and then make a list from them to use in a shell script loop. Seems to error on the "set Switches to paragraphs of (read POSIX file file path)" and I assume I need to reconstruct but I know Applescript and barely getting into ObjC so this one has me stumped....
I really want to change the first three lines to set filepath to choose file but I get the same error.
on theButtonChooseFile_(sender)
set x to (system info)
set theuser to short user name of x
set filepath to "/Users/" & theuser & "/Desktop/switches.txt" as string -- Define path to file
set listOfSwitches to {}
set Switches to paragraphs of (read POSIX file filepath)
repeat with nextLine in Switches
if length of nextLine is greater than 0 then
copy nextLine to the end of listOfSwitches
end if
end repeat
end theButtonChooseFile_
Error:
2016-08-25 08:13:37.655 Cisco Configurator[67884:4593159] *** -[AppDelegate theButtonChooseFile:]: Can’t make current application into type file. (error -1700)
The AppleScript read command accepts also POSIX paths and you can get the path to the desktop folder of the current user simply with
path to desktop
The entire handler can be written this way
on theButtonChooseFile_(sender)
set desktopFolder to POSIX path of (path to desktop)
set filepath to desktopFolder & "switches.txt"
set Switches to paragraphs of (read filepath)
set listOfSwitches to {}
repeat with nextLine in Switches
if length of nextLine is greater than 0 then
copy nextLine to the end of listOfSwitches
end if
end repeat
end theButtonChooseFile_
But consider than listOfSwitches is only accessible in the handler.
I really hope someone can help me out with this. I recently moved from one mac to another and did a clean install. I have several folders with hundreds of alias (how do you say the plural of alias?)...
The original file path is "/Volumes/Media Drive/Ableton/Warped Tracks/", and the new path needs to be "/users/joel/Music/Ableton Projects/Warped Tracks"
I see how to fix them one at a time, but that would take hours. I tried this applescript, but had no luck:
https://apple.stackexchange.com/questions/2656/how-do-i-fix-failed-aliases
Can anyone give me a better applescript or another solution? As I mentioned, I tried this applescript:
tell application "Finder"
set these_items to the selection
end tell
repeat with i from 1 to the count of these_items
set this_item to (item i of these_items) as alias
set this_info to info for this_item
if class of this_item is alias then
tell application "Finder"
set original_file to original item of this_item
set this_alias_file_name to displayed name of this_item
set container_folder to container of this_item
set the_path to the POSIX path of (original_file as alias)
set new_path to my replaceText("/Volumes/Media Drive/Ableton/Warped Tracks/", "/users/joel/Music/Ableton Projects/Warped Tracks", the_path)
move this_item to trash
try
make new alias file at container_folder to (POSIX file new_path) with properties {name:this_alias_file_name}
on error errMsg number errorNumber
if errorNumber is -10000 then -- new original file not found, try relinking to old
try
make new alias file at container_folder to (POSIX file the_path) with properties {name:this_alias_file_name}
on error errMsg number errorNumber
if errorNumber is -10000 then -- old original not found. link's dead Jim
display dialog "The original file for alias " & this_alias_file_name & " was not found."
else
display dialog "An unknown error occurred: " & errorNumber as text
end if
end try
else
display dialog "An unknown error occurred: " & errorNumber as text
end if
end try
end tell
end if
end repeat
on replaceText(find, replace, subject)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set subject to text items of subject
set text item delimiters of AppleScript to replace
set subject to "" & subject
set text item delimiters of AppleScript to prevTIDs
return subject
end replaceText
Any help would be greatly appreciated.
Edit: I think the problem with the applescript for me is that I have folders further down the path that all are different, and THOSE each contain the individual alias files. IE:
"/users/joel/Music/Ableton Projects/Warped Tracks/Folder A/file.alias",/users/joel/Music/Ableton Projects/Warped Tracks/Folder B/file2.alias", etc, etc
I had same issue years ago (transfer between 2 media centers), and I made this program : it asks for folder were all broken alias are and then try to find again original path to rebuild them.
the thing is that any applescript instruction about alias gives error when link is broken, so you must use Finder info window to read original path without error when the link is already broken:
(this program assumes that original file name is unique)
-- Select main folder
tell application "Finder" to set Mon_Dossier to ((choose folder with prompt "Select top folder:" without invisibles) as alias) as string
-- repair alias in this folder and all subsequent folders (entire contents)
tell application "Finder" to set Mes_Alias to every file of entire contents of folder Mon_Dossier whose class is alias file
-- loop on each alias of the folder
repeat with Mon_Alias in Mes_Alias
-- found the original file path of Mon_Alias
-- try first with standard alias call
set F_Source to ""
try
tell application "Finder" to set F_Source to original item of Mon_Alias
end try
if F_Source is "" then
-- the link to original file is broken, then use on windows info method
set F_Source to Origine_Alias(Mon_Alias)
set F_Original to Decompose(F_Source)
-- no need to look original file as is, becuase the link is broken (path changed ?)
-- then directly search for the same file without extension
set Cible to ((chemin of F_Original) as string) & ((Nom of F_Original) as string)
tell application "Finder"
if exists file Cible then
-- file is found without extension
-- then delete alias and create new alias with same name and in same folder
set A_Nom to name of Mon_Alias
set A_Dossier to folder of Mon_Alias
delete Mon_Alias
set Nouvel_Alias to make alias to Cible at A_Dossier
set name of Nouvel_Alias to A_Nom
else
SLog("Alias=" & (Mon_Alias as string) & " File not found=" & Cible)
end if
end tell
else
-- the alias link is still valid : nothing to do ! go to next alias..
end if
end repeat
-- end main program
-- sub routine to find passe of broken link (original path/file can't be found)
-- the result is a unix path like folder/sub_folder/file
on Origine_Alias(F_Alias)
set R to ""
tell application "Finder" to open information window of file (F_Alias as text)
tell application "System Events" to tell process "Finder" to set R to value of static text 19 of scroll area 1 of front window
tell application "Finder" to close front window
return R
end Origine_Alias
-- sub routine to extract, from unix file path, the path, the file name and its extension: result is sent back in a record
-- Warning : we can't use "Posix file of" becuase the path and the file are non longer valid ! (then Posix gives error)
on Decompose(Local_F)
--search the first "." from right to find extension
set X to length of Local_F
repeat while (character X of Local_F is not ".") and (X > 0)
set X to X - 1
end repeat
if X > 0 then
set L_Ext to text (X + 1) thru (length of Local_F) of Local_F
set Local_F to text 1 thru (X - 1) of Local_F
else
L_Ext = "" -- "." not found, then no extension !
end if
-- search first "/" from the right to extract file name
set X to length of Local_F
repeat while (character X of Local_F is not "/")
set X to X - 1
end repeat
set L_Nom to text (X + 1) thru (length of Local_F) of Local_F
set Local_F to text 1 thru (X) of Local_F
try
set L_Chemin to POSIX file Local_F
end try
return {chemin:L_Chemin, Nom:L_Nom, Ext:L_Ext}
end Decompose
-- sub routine log (text log file on desktop)
on SLog(msg)
set the my_log to ¬
((path to desktop) as text) & "My_logfile.txt"
try
-- open file. create it if not yet exist
open for access file the my_log with write permission
-- add text at end of file
write (msg & return) to file the my_log starting at eof
close access file the my_log
on error
try
close access file the my_log
end try
end try
end SLog
I'm trying to move a bunch of files into folders, based on the file creation date. Using the script below, the rate of move is about 1 file per second. 64000 files to go.
Script timed out when I tried
set myFiles to items of myFolder whose name ends with "jpg"
repeat with aFile in myFiles
So now I process file by file and only checking folder existing when the date comparison failed sped up the process only marginally. I've tried the obvious
set myDayBuddies to items of myFolder whose creation date is theFileDate
but this too, did not help really much. I suspect Finder reads the whole folder content time and time again, creating huge overhead.
What is the proper method of speeding up this any further? In PHP I know I can read entries from a directory stream, one by one.
Script:
tell application "Finder"
set myFolder to choose folder
set prevDateString to ""
repeat while true
try
set aFile to first item of myFolder whose name ends with "jpg"
set theFileDate to (the creation date of aFile)
set theDateString to my composedate(theFileDate)
if (theDateString is not prevDateString) then
if not (exists folder ((myFolder & theDateString) as text)) then
make new folder at myFolder with properties {name:theDateString}
end if
set prevDateString to theDateString
set destPath to (((myFolder as text) & theDateString) as alias)
end if
move aFile to destPath
on error
return
end try
end repeat
end tell
on composedate(aDate)
set y to (year of aDate as integer) as string
set m to (month of aDate as integer) as string
set d to (day of aDate as integer) as string
if length of m < 2 then
set m to "0" & m
end if
if length of d < 2 then
set d to "0" & d
end if
set myDateString to y & m & d
return myDateString
end composedate
System Events was created to take much of the load off the Finder. The Finder is slow so use system events when possible. I'm not sure how it will perform on a folder with 64000 files but give it a try.
set myFolder to choose folder
tell application "System Events"
set f to files of myFolder whose name extension is "jpg"
end tell
And if system events is still too slow then applescript alone isn't appropriate for such a large folder. You'll have to go to using the command line even though you said you don't want to. Something like this should be faster...
set myFolder to (choose folder) as text
set jpgFiles to paragraphs of (do shell script "ls " & quoted form of POSIX path of myFolder & " | grep 'jpg$'")
repeat with aFile in jpgFiles
set filePath to myFolder & aFile
tell application "System Events"
set theFileDate to creation date of file filePath
-- do more stuff
end tell
end repeat
I'd like a little help fine tuning this script. Is it possible to have this script match subfolders based on the first two letters of file? Parent folder would be "Shots" which contains subfolders with unique two letter prefix "BA_Bikini_Atol" which contains subfolders within those folders for the specific shots ba_0020, ba_0030, etc. Would like to move a file, ba_0020_v0002 to the ba_0020 folder by selecting "shots" as the target and the script looks through all the sub folders for a match. Thoughts?
set mgFilesFolder to (choose folder with prompt "Where are the files stored which you would like to move to the destination?")
set mgDestFolder to (choose folder with prompt "Where is the destination folder?")
tell application "System Events"
set folderList to name of folders of mgDestFolder
set fileList to name of files of mgFilesFolder
end tell
repeat with i from 1 to (count folderList)
set folderName to item i of folderList
set filesToMove to {}
repeat with j from 1 to (count fileList)
set filename to item j of fileList
if filename begins with folderName then
set end of filesToMove to alias ((mgFilesFolder as string) & filename)
end if
end repeat
tell application "Finder"
move filesToMove to alias ((mgDestFolder as string) & folderName & ":")
end tell
end repeat
This works in my tests
set mgFilesFolder to (choose folder with prompt "Where are the files stored which you would like to move to the destination?")
set mgDestFolder to (choose folder with prompt "Where is the destination folder?")
(* get the files of the mgFilesFolder folder *)
tell application "Finder" to set fileList to files of mgFilesFolder
(* iterate over every file *)
repeat with i from 1 to number of items in fileList
set this_item to item i of fileList
set this_file_Name to name of this_item as string
(* get the file name code
Using the delimiter "_" break the name into fields of text and returning fields 1 thru 2 . i.e ba_0030 *)
set thisFileCode to (do shell script "echo " & quoted form of this_file_Name & " |cut -d _ -f 1-2")
log thisFileCode
tell application "Finder"
set folderList to ((folders of entire contents of mgDestFolder) whose name starts with thisFileCode)
if folderList is not {} then
move this_item to item 1 of folderList
end if
end tell
end repeat
FROM:
TO:
I've made (with the help from users of this site) the script you're referring to.
I've made similar (more efficient) scripts since then. Most of them depend on ASObjC Runner which you can get here: Download ASObjC Runner
If you have it installed I think the following script will work:
set mgFilesFolder to (choose folder with prompt "Where are the file stored which you would like to move?")
set mgDestFolder to (choose folder with prompt "Where are destination folders?")
tell application "ASObjC Runner"
set mgFiles to enumerate folder mgFilesFolder with recursion without including folders
repeat with mgFile in mgFiles
set mgPrefix to text 1 thru 7 of name of (about file mgFile include only "name")
set mgDest to enumerate folder mgDestFolder match prefix mgPrefix with recursion
manage file mgFile moving into mgDest
end repeat
end tell
I have an applescript where the user will pick one file and i need to get the name of that file minus the extension.
I found a post on a different site that said this would work:
tell application "Finder"
set short_name to name of selected_file
end tell
But it returns the extensions as well. How can I get just the name of the file?
You can ask the Finder or System Events for the name extension and remove that part from the full name. This will also avoid issues with names that have periods in them or extensions that may not be what you think they are.
set someItem to (choose file)
tell application "System Events" to tell disk item (someItem as text) to set {theName, theExtension} to {name, name extension}
if theExtension is not "" then set theName to text 1 thru -((count theExtension) + 2) of theName -- the name part
log theName & tab & theExtension
This should work with filenames that contain periods and ones that don't have an extension (but not both). It returns the last extension for files that have multiple extensions.
tell application "Finder"
set n to name of file "test.txt" of desktop
set AppleScript's text item delimiters to "."
if number of text items of n > 1 then
set n to text items 1 thru -2 of n as text
end if
n
end tell
name extension also returns the last extension for files with more than one extension:
tell application "Finder"
name extension of file "archive.tar.gz" of desktop -- gz
end tell
Here's the script for getting just the filenames.
set filesFound to {}
set filesFound2 to {}
set nextItem to 1
tell application "Finder"
set myFiles to name of every file of (path to desktop) --change path to whatever path you want
end tell
--loop used for populating list filesFound with all filenames found (name + extension)
repeat with i in myFiles
set end of filesFound to (item nextItem of myFiles)
set nextItem to (nextItem + 1)
end repeat
set nextItem to 1 --reset counter to 1
--loop used for pulling each filename from list filesFound and then strip the extension
--from filename and populate a new list called filesFound2
repeat with i in filesFound
set myFile2 to item nextItem of filesFound
set myFile3 to text 1 thru ((offset of "." in myFile2) - 1) of myFile2
set end of filesFound2 to myFile3
set nextItem to (nextItem + 1)
end repeat
return filesFound2
This script uses ASObjC Runner to parse file paths. Download ASObjC Runner here.
set aFile to choose file
tell application "ASObjC Runner" to set nameS to name stub of (parsed path of (aFile as text))