2015-11-15 2 views
0

Мне нужно, чтобы AppleScript знал имя файла, выбранного пользователем в команде choose file. Похоже, это должно быть очень просто, но я не могу понять ответ. Сценарий извлекает кадры из файла gif и помещает отдельные изображения в папку внутри содержимого приложения. Затем он быстро меняет фон рабочего стола на изображения внутри папки, поэтому дает вам gif для обоев. Однако я не могу этого сделать, не зная имя выбранного файла gif, так как не знаю имен изображений в папке. Если есть что-то еще, это будет здорово. Это то, что я до сих пор:Получение имени выбранного файла в applescript

on delay duration 
set endTime to (current date) + duration 
repeat while (current date) is less than endTime 
    tell AppleScript to delay duration 
end repeat 
end delay 
set gifFiles to choose file of type "com.compuserve.gif" with prompt "Select GIF" 
set dest to quoted form of POSIX path of ((path to me as string) & "Contents:Resources:Gif") 
set pScript to quoted form of "from AppKit import NSApplication, NSImage, NSImageCurrentFrame, NSGIFFileType; import sys, os 
tName=os.path.basename(sys.argv[1]) 
dir=sys.argv[2] 
app=NSApplication.sharedApplication() 
img=NSImage.alloc().initWithContentsOfFile_(sys.argv[1]) 
if img: 
gifRep=img.representations()[0] 
frames=gifRep.valueForProperty_('NSImageFrameCount') 
if frames: 
    for i in range(frames.intValue()): 
     gifRep.setProperty_withValue_(NSImageCurrentFrame, i) 
     gifRep.representationUsingType_properties_(NSGIFFileType, None).writeToFile_atomically_(dir + tName + ' ' + str(i + 1).zfill(2) + '.gif', True) 
    print (i + 1)" 
repeat with f in gifFiles 
set numberOfExtractedGIFs to (do shell script "/usr/bin/python -c " & pScript & " " & (quoted form of POSIX path of f) & " " & dest) as integer 
end repeat 
repeat 
set desktop_image to (path to me as string) & "Contents:Resources:Gif:" 
tell application "Finder" to set the desktop picture to desktop_image 
delay 0.05 
set desktop_image to (path to me as string) & "Contents:Resources:Gif:" 
tell application "Finder" to set the desktop picture to desktop_image 
delay 0.05 
set desktop_image to (path to me as string) & "Contents:Resources:Gif:" 
tell application "Finder" to set the desktop picture to desktop_image 
delay 0.05 
set desktop_image to (path to me as string) & "Contents:Resources:Gif:" 
tell application "Finder" to set the desktop picture to desktop_image 
delay 0.05 
set desktop_image to (path to me as string) & "Contents:Resources:Gif:" 
tell application "Finder" to set the desktop picture to desktop_image 

ответ

0

Вы можете получить имя с System Events

set gifFiles to choose file of type "com.compuserve.gif" with prompt "Select GIF" 
tell application "System Events" to set gifFileName to name of gifFiles 

или с помощью команды info for, хотя это не рекомендуется, так как лет она по-прежнему работают даже в Эль Капитане

set gifFiles to choose file of type "com.compuserve.gif" with prompt "Select GIF" 
set gifFileName to name of (info for gifFiles) 

Конечно, это возможно с помощью Finder, но рекомендуется избегать привлечения Finder как можно больше

+0

Похоже, что это работает, поэтому спасибо, но я столкнулся с новой проблемой: установите dest для цитированной формы пути POSIX ((путь ко мне как строки) и «Содержание: Ресурсы: Gif») т работы. Это не дает мне сообщение об ошибке, просто это просто не делает. Ближайший, на мой взгляд, я пришел: set dest для цитированной формы пути POSIX и (путь ко мне) & "Contents: Resources: Gif". Если вы можете понять, что с этим не так, это было бы здорово. Благодарю. –

+0

Вы должны поставить все выражение, которое должно быть указано в круглых скобках: 'set dest to quoted form (путь POSIX (путь к мне) и« Содержание/Ресурсы/Gif »)', а компоненты литерала должны быть разделенных косой чертой. – vadian

0

Чтобы получить имя файла, вы должны использовать свойство имени Finder:

set gifFiles to choose file of type "com.compuserve.gif" with prompt "Select GIF" 
tell application "Finder" to set myName to name of file gifFiles 

Переменная Myname содержит имя выбранного файла (с расширением).

Смежные вопросы